home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Modules / _cursesmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-27  |  63.7 KB  |  2,358 lines

  1. /*
  2.  *   This is a curses module for Python.
  3.  *
  4.  *   Based on prior work by Lance Ellinghaus and Oliver Andrich
  5.  *   Version 1.2 of this module: Copyright 1994 by Lance Ellinghouse,
  6.  *    Cathedral City, California Republic, United States of America.
  7.  *
  8.  *   Version 1.5b1, heavily extended for ncurses by Oliver Andrich:
  9.  *   Copyright 1996,1997 by Oliver Andrich, Koblenz, Germany.
  10.  *
  11.  *   Tidied for Python 1.6, and currently maintained by AMK (amk1@bigfoot.com)
  12.  *
  13.  *   Permission is hereby granted, free of charge, to any person obtaining
  14.  *   a copy of this source file to use, copy, modify, merge, or publish it
  15.  *   subject to the following conditions:
  16.  *
  17.  *   The above copyright notice and this permission notice shall be included
  18.  *   in all copies or in any new file that contains a substantial portion of
  19.  *   this file.
  20.  *
  21.  *   THE  AUTHOR  MAKES  NO  REPRESENTATIONS ABOUT  THE  SUITABILITY  OF
  22.  *   THE  SOFTWARE FOR  ANY  PURPOSE.  IT IS  PROVIDED  "AS IS"  WITHOUT
  23.  *   EXPRESS OR  IMPLIED WARRANTY.  THE AUTHOR DISCLAIMS  ALL WARRANTIES
  24.  *   WITH  REGARD TO  THIS  SOFTWARE, INCLUDING  ALL IMPLIED  WARRANTIES
  25.  *   OF   MERCHANTABILITY,  FITNESS   FOR  A   PARTICULAR  PURPOSE   AND
  26.  *   NON-INFRINGEMENT  OF THIRD  PARTY  RIGHTS. IN  NO  EVENT SHALL  THE
  27.  *   AUTHOR  BE LIABLE  TO  YOU  OR ANY  OTHER  PARTY  FOR ANY  SPECIAL,
  28.  *   INDIRECT,  OR  CONSEQUENTIAL  DAMAGES  OR  ANY  DAMAGES  WHATSOEVER
  29.  *   WHETHER IN AN  ACTION OF CONTRACT, NEGLIGENCE,  STRICT LIABILITY OR
  30.  *   ANY OTHER  ACTION ARISING OUT OF  OR IN CONNECTION WITH  THE USE OR
  31.  *   PERFORMANCE OF THIS SOFTWARE.
  32.  */
  33.  
  34. /* CVS: $Id: _cursesmodule.c,v 2.37 2000/07/27 11:58:01 akuchling Exp $ */
  35.  
  36. /*
  37.  
  38. A number of SysV or ncurses functions don't have wrappers yet; if you need
  39. a given function, add it and send a patch.  Here's a list of currently
  40. unsupported functions:
  41.  
  42.     addchnstr addchstr chgat color_set copywin define_key
  43.     del_curterm delscreen dupwin inchnstr inchstr innstr keyok
  44.     mcprint mvaddchnstr mvaddchstr mvchgat mvcur mvinchnstr
  45.     mvinchstr mvinnstr mmvwaddchnstr mvwaddchstr mvwchgat
  46.     mvwgetnstr mvwinchnstr mvwinchstr mvwinnstr napms newterm
  47.     overlay overwrite resetty resizeterm restartterm ripoffline
  48.     savetty scr_dump scr_init scr_restore scr_set scrl set_curterm
  49.     set_term setterm setupterm tgetent tgetflag tgetnum tgetstr
  50.     tgoto timeout tparm tputs tputs typeahead use_default_colors
  51.     vidattr vidputs waddchnstr waddchstr wchgat wcolor_set
  52.     winchnstr winchstr winnstr wmouse_trafo wredrawln wscrl
  53.     wtimeout
  54.  
  55. Low-priority: 
  56.     slk_attr slk_attr_off slk_attr_on slk_attr_set slk_attroff
  57.     slk_attron slk_attrset slk_clear slk_color slk_init slk_label
  58.     slk_noutrefresh slk_refresh slk_restore slk_set slk_touch
  59.  
  60.  */
  61.  
  62. /* Release Number */
  63.  
  64. char *PyCursesVersion = "1.6";
  65.  
  66. /* Includes */
  67.  
  68. #include "Python.h"
  69.  
  70. #ifdef __osf__
  71. #define _XOPEN_SOURCE_EXTENDED  /* Define macro for OSF/1 */
  72. #define STRICT_SYSV_CURSES      /* Don't use ncurses extensions */
  73. #endif
  74.  
  75. #ifdef HAVE_NCURSES_H
  76. #include <ncurses.h>
  77. #else
  78. #include <curses.h>
  79. #endif
  80.  
  81. #if defined(__sgi__) || defined(__sun__)
  82. #define STRICT_SYSV_CURSES       /* Don't use ncurses extensions */
  83. typedef chtype attr_t;           /* No attr_t type is available */
  84. #endif
  85.  
  86. /* Definition of exception curses.error */
  87.  
  88. static PyObject *PyCursesError;
  89.  
  90. /* general error messages */
  91. static char *catchall_ERR  = "curses function returned ERR";
  92. static char *catchall_NULL = "curses function returned NULL";
  93.  
  94. /* Tells whether initscr() has been called to initialise curses.  */
  95. static int initialised = FALSE;
  96.  
  97. /* Tells whether start_color() has been called to initialise colorusage. */
  98. static int initialisedcolors = FALSE;
  99.  
  100. /* Utility Macros */
  101. #define ARG_COUNT(X) \
  102.     (((X) == NULL) ? 0 : (PyTuple_Check(X) ? PyTuple_Size(X) : 1))
  103.  
  104. #define PyCursesInitialised \
  105.   if (initialised != TRUE) { \
  106.                   PyErr_SetString(PyCursesError, \
  107.                                   "must call initscr() first"); \
  108.                   return NULL; }
  109.  
  110. #define PyCursesInitialisedColor \
  111.   if (initialisedcolors != TRUE) { \
  112.                   PyErr_SetString(PyCursesError, \
  113.                                   "must call start_color() first"); \
  114.                   return NULL; }
  115.  
  116. /* Utility Functions */
  117.  
  118. /*
  119.  * Check the return code from a curses function and return None 
  120.  * or raise an exception as appropriate.
  121.  */
  122.  
  123. static PyObject *
  124. PyCursesCheckERR(int code, char *fname)
  125. {
  126.   if (code != ERR) {
  127.     Py_INCREF(Py_None);
  128.     return Py_None;
  129.   } else {
  130.     if (fname == NULL) {
  131.       PyErr_SetString(PyCursesError, catchall_ERR);
  132.     } else {
  133.       PyErr_Format(PyCursesError, "%s() returned ERR", fname);
  134.     }
  135.     return NULL;
  136.   }
  137. }
  138.  
  139. static int 
  140. PyCurses_ConvertToChtype(PyObject *obj, chtype *ch)
  141. {
  142.   if (PyInt_Check(obj)) {
  143.     *ch = (chtype) PyInt_AsLong(obj);
  144.   } else if(PyString_Check(obj) &
  145.         (PyString_Size(obj) == 1)) {
  146.     *ch = (chtype) *PyString_AsString(obj);
  147.   } else {
  148.     return 0;
  149.   }
  150.   return 1;
  151. }
  152.  
  153. /*****************************************************************************
  154.  The Window Object
  155. ******************************************************************************/
  156.  
  157. /* Definition of the window object and window type */
  158.  
  159. typedef struct {
  160.     PyObject_HEAD
  161.     WINDOW *win;
  162. } PyCursesWindowObject;
  163.  
  164. PyTypeObject PyCursesWindow_Type;
  165.  
  166. #define PyCursesWindow_Check(v)     ((v)->ob_type == &PyCursesWindow_Type)
  167.  
  168. /* Function Prototype Macros - They are ugly but very, very useful. ;-)
  169.  
  170.    X - function name
  171.    TYPE - parameter Type
  172.    ERGSTR - format string for construction of the return value
  173.    PARSESTR - format string for argument parsing
  174.    */
  175.  
  176. #define Window_NoArgNoReturnFunction(X) \
  177. static PyObject *PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
  178. { if (!PyArg_NoArgs(args)) return NULL; \
  179.   return PyCursesCheckERR(X(self->win), # X); }
  180.  
  181. #define Window_NoArgTrueFalseFunction(X) \
  182. static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
  183. { \
  184.   if (!PyArg_NoArgs(args)) return NULL; \
  185.   if (X (self->win) == FALSE) { Py_INCREF(Py_False); return Py_False; } \
  186.   else { Py_INCREF(Py_True); return Py_True; } }
  187.  
  188. #define Window_NoArgNoReturnVoidFunction(X) \
  189. static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
  190. { \
  191.   if (!PyArg_NoArgs(args)) return NULL; \
  192.   X(self->win); Py_INCREF(Py_None); return Py_None; }
  193.  
  194. #define Window_NoArg2TupleReturnFunction(X, TYPE, ERGSTR) \
  195. static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
  196. { \
  197.   TYPE arg1, arg2; \
  198.   if (!PyArg_NoArgs(args)) return NULL; \
  199.   X(self->win,arg1,arg2); return Py_BuildValue(ERGSTR, arg1, arg2); } 
  200.  
  201. #define Window_OneArgNoReturnVoidFunction(X, TYPE, PARSESTR) \
  202. static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
  203. { \
  204.   TYPE arg1; \
  205.   if (!PyArg_Parse(args, PARSESTR, &arg1)) return NULL; \
  206.   X(self->win,arg1); Py_INCREF(Py_None); return Py_None; }
  207.  
  208. #define Window_OneArgNoReturnFunction(X, TYPE, PARSESTR) \
  209. static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
  210. { \
  211.   TYPE arg1; \
  212.   if (!PyArg_Parse(args,PARSESTR, &arg1)) return NULL; \
  213.   return PyCursesCheckERR(X(self->win, arg1), # X); }
  214.  
  215. #define Window_TwoArgNoReturnFunction(X, TYPE, PARSESTR) \
  216. static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
  217. { \
  218.   TYPE arg1, arg2; \
  219.   if (!PyArg_Parse(args,PARSESTR, &arg1, &arg2)) return NULL; \
  220.   return PyCursesCheckERR(X(self->win, arg1, arg2), # X); }
  221.  
  222. /* ------------- WINDOW routines --------------- */
  223.  
  224. Window_NoArgNoReturnFunction(untouchwin)
  225. Window_NoArgNoReturnFunction(touchwin)
  226. Window_NoArgNoReturnFunction(redrawwin)
  227. Window_NoArgNoReturnFunction(winsertln)
  228. Window_NoArgNoReturnFunction(werase)
  229. Window_NoArgNoReturnFunction(wdeleteln)
  230.  
  231. Window_NoArgTrueFalseFunction(is_wintouched)
  232.  
  233. Window_NoArgNoReturnVoidFunction(wsyncup)
  234. Window_NoArgNoReturnVoidFunction(wsyncdown)
  235. Window_NoArgNoReturnVoidFunction(wstandend)
  236. Window_NoArgNoReturnVoidFunction(wstandout)
  237. Window_NoArgNoReturnVoidFunction(wcursyncup)
  238. Window_NoArgNoReturnVoidFunction(wclrtoeol)
  239. Window_NoArgNoReturnVoidFunction(wclrtobot)
  240. Window_NoArgNoReturnVoidFunction(wclear)
  241.  
  242. Window_OneArgNoReturnVoidFunction(idcok, int, "i;True(1) or False(0)")
  243. Window_OneArgNoReturnVoidFunction(immedok, int, "i;True(1) or False(0)")
  244. Window_OneArgNoReturnVoidFunction(wtimeout, int, "i;delay")
  245.  
  246. Window_NoArg2TupleReturnFunction(getyx, int, "(ii)")
  247. Window_NoArg2TupleReturnFunction(getbegyx, int, "(ii)")
  248. Window_NoArg2TupleReturnFunction(getmaxyx, int, "(ii)")
  249. Window_NoArg2TupleReturnFunction(getparyx, int, "(ii)")
  250.  
  251. Window_OneArgNoReturnFunction(wattron, attr_t, "l;attr")
  252. Window_OneArgNoReturnFunction(wattroff, attr_t, "l;attr")
  253. Window_OneArgNoReturnFunction(wattrset, attr_t, "l;attr")
  254. Window_OneArgNoReturnFunction(clearok, int, "i;True(1) or False(0)")
  255. Window_OneArgNoReturnFunction(idlok, int, "i;True(1) or False(0)")
  256. Window_OneArgNoReturnFunction(keypad, int, "i;True(1) or False(0)")
  257. Window_OneArgNoReturnFunction(leaveok, int, "i;True(1) or False(0)")
  258. Window_OneArgNoReturnFunction(nodelay, int, "i;True(1) or False(0)")
  259. Window_OneArgNoReturnFunction(notimeout, int, "i;True(1) or False(0)")
  260. Window_OneArgNoReturnFunction(scrollok, int, "i;True(1) or False(0)")
  261. Window_OneArgNoReturnFunction(winsdelln, int, "i;nlines")
  262. Window_OneArgNoReturnFunction(syncok, int, "i;True(1) or False(0)")
  263.  
  264. Window_TwoArgNoReturnFunction(mvwin, int, "(ii);y,x")
  265. Window_TwoArgNoReturnFunction(mvderwin, int, "(ii);y,x")
  266. Window_TwoArgNoReturnFunction(wmove, int, "(ii);y,x")
  267. #ifndef STRICT_SYSV_CURSES
  268. Window_TwoArgNoReturnFunction(wresize, int, "(ii);lines,columns")
  269. #endif
  270.  
  271. /* Allocation and deallocation of Window Objects */
  272.  
  273. static PyObject *
  274. PyCursesWindow_New(WINDOW *win)
  275. {
  276.     PyCursesWindowObject *wo;
  277.  
  278.     wo = PyObject_NEW(PyCursesWindowObject, &PyCursesWindow_Type);
  279.     if (wo == NULL) return NULL;
  280.     wo->win = win;
  281.     return (PyObject *)wo;
  282. }
  283.  
  284. static void
  285. PyCursesWindow_Dealloc(PyCursesWindowObject *wo)
  286. {
  287.   if (wo->win != stdscr) delwin(wo->win);
  288.   PyMem_DEL(wo);
  289. }
  290.  
  291. /* Addch, Addstr, Addnstr */
  292.  
  293. static PyObject *
  294. PyCursesWindow_AddCh(PyCursesWindowObject *self, PyObject *args)
  295. {
  296.   int rtn, x, y, use_xy = FALSE;
  297.   PyObject *temp;
  298.   chtype ch = 0;
  299.   attr_t attr = A_NORMAL;
  300.   
  301.   switch (ARG_COUNT(args)) {
  302.   case 1:
  303.     if (!PyArg_Parse(args, "O;ch or int", &temp))
  304.       return NULL;
  305.     break;
  306.   case 2:
  307.     if (!PyArg_Parse(args, "(Ol);ch or int,attr", &temp, &attr))
  308.       return NULL;
  309.     break;
  310.   case 3:
  311.     if (!PyArg_Parse(args,"(iiO);y,x,ch or int", &y, &x, &temp))
  312.       return NULL;
  313.     use_xy = TRUE;
  314.     break;
  315.   case 4:
  316.     if (!PyArg_Parse(args,"(iiOl);y,x,ch or int, attr", 
  317.              &y, &x, &temp, &attr))
  318.       return NULL;
  319.     use_xy = TRUE;
  320.     break;
  321.   default:
  322.     PyErr_SetString(PyExc_TypeError, "addch requires 1 or 4 arguments");
  323.     return NULL;
  324.   }
  325.  
  326.   if (!PyCurses_ConvertToChtype(temp, &ch)) {
  327.     PyErr_SetString(PyExc_TypeError, "argument 1 or 3 must be a ch or an int");
  328.     return NULL;
  329.   }
  330.   
  331.   if (use_xy == TRUE)
  332.     rtn = mvwaddch(self->win,y,x, ch | attr);
  333.   else {
  334.     rtn = waddch(self->win, ch | attr);
  335.   }
  336.   return PyCursesCheckERR(rtn, "addch");
  337. }
  338.  
  339. static PyObject *
  340. PyCursesWindow_AddStr(PyCursesWindowObject *self, PyObject *args)
  341. {
  342.   int rtn;
  343.   int x, y;
  344.   char *str;
  345.   attr_t attr = A_NORMAL , attr_old = A_NORMAL;
  346.   int use_xy = FALSE, use_attr = FALSE;
  347.  
  348.   switch (ARG_COUNT(args)) {
  349.   case 1:
  350.     if (!PyArg_Parse(args,"s;str", &str))
  351.       return NULL;
  352.     break;
  353.   case 2:
  354.     if (!PyArg_Parse(args,"(sl);str,attr", &str, &attr))
  355.       return NULL;
  356.     use_attr = TRUE;
  357.     break;
  358.   case 3:
  359.     if (!PyArg_Parse(args,"(iis);int,int,str", &y, &x, &str))
  360.       return NULL;
  361.     use_xy = TRUE;
  362.     break;
  363.   case 4:
  364.     if (!PyArg_Parse(args,"(iisl);int,int,str,attr", &y, &x, &str, &attr))
  365.       return NULL;
  366.     use_xy = use_attr = TRUE;
  367.     break;
  368.   default:
  369.     PyErr_SetString(PyExc_TypeError, "addstr requires 1 to 4 arguments");
  370.     return NULL;
  371.   }
  372.  
  373.   if (use_attr == TRUE) {
  374.     attr_old = getattrs(self->win);
  375.     wattrset(self->win,attr);
  376.   }
  377.   if (use_xy == TRUE)
  378.     rtn = mvwaddstr(self->win,y,x,str);
  379.   else
  380.     rtn = waddstr(self->win,str);
  381.   if (use_attr == TRUE)
  382.     wattrset(self->win,attr_old);
  383.   return PyCursesCheckERR(rtn, "addstr");
  384. }
  385.  
  386. static PyObject *
  387. PyCursesWindow_AddNStr(PyCursesWindowObject *self, PyObject *args)
  388. {
  389.   int rtn, x, y, n;
  390.   char *str;
  391.   attr_t attr = A_NORMAL , attr_old = A_NORMAL;
  392.   int use_xy = FALSE, use_attr = FALSE;
  393.  
  394.   switch (ARG_COUNT(args)) {
  395.   case 2:
  396.     if (!PyArg_Parse(args,"(si);str,n", &str, &n))
  397.       return NULL;
  398.     break;
  399.   case 3:
  400.     if (!PyArg_Parse(args,"(sil);str,n,attr", &str, &n, &attr))
  401.       return NULL;
  402.     use_attr = TRUE;
  403.     break;
  404.   case 4:
  405.     if (!PyArg_Parse(args,"(iisi);y,x,str,n", &y, &x, &str, &n))
  406.       return NULL;
  407.     use_xy = TRUE;
  408.     break;
  409.   case 5:
  410.     if (!PyArg_Parse(args,"(iisil);y,x,str,n,attr", &y, &x, &str, &n, &attr))
  411.       return NULL;
  412.     use_xy = use_attr = TRUE;
  413.     break;
  414.   default:
  415.     PyErr_SetString(PyExc_TypeError, "addnstr requires 2 to 5 arguments");
  416.     return NULL;
  417.   }
  418.  
  419.   if (use_attr == TRUE) {
  420.     attr_old = getattrs(self->win);
  421.     wattrset(self->win,attr);
  422.   }
  423.   if (use_xy == TRUE)
  424.     rtn = mvwaddnstr(self->win,y,x,str,n);
  425.   else
  426.     rtn = waddnstr(self->win,str,n);
  427.   if (use_attr == TRUE)
  428.     wattrset(self->win,attr_old);
  429.   return PyCursesCheckERR(rtn, "addnstr");
  430. }
  431.  
  432. static PyObject *
  433. PyCursesWindow_Bkgd(PyCursesWindowObject *self, PyObject *args)
  434. {
  435.   PyObject *temp;
  436.   chtype bkgd;
  437.   attr_t attr = A_NORMAL;
  438.  
  439.   switch (ARG_COUNT(args)) {
  440.     case 1:
  441.       if (!PyArg_Parse(args, "O;ch or int", &temp))
  442.         return NULL;
  443.       break;
  444.     case 2:
  445.       if (!PyArg_Parse(args,"(Ol);ch or int,attr", &temp, &attr))
  446.         return NULL;
  447.       break;
  448.     default:
  449.       PyErr_SetString(PyExc_TypeError, "bkgd requires 1 or 2 arguments");
  450.       return NULL;
  451.   }
  452.  
  453.   if (!PyCurses_ConvertToChtype(temp, &bkgd)) {
  454.     PyErr_SetString(PyExc_TypeError, "argument 1 or 3 must be a ch or an int");
  455.     return NULL;
  456.   }
  457.  
  458.   return PyCursesCheckERR(wbkgd(self->win, bkgd | A_NORMAL), "bkgd");
  459. }
  460.  
  461. static PyObject *
  462. PyCursesWindow_BkgdSet(PyCursesWindowObject *self, PyObject *args)
  463. {
  464.   PyObject *temp;
  465.   chtype bkgd;
  466.   attr_t attr = A_NORMAL;
  467.  
  468.   switch (ARG_COUNT(args)) {
  469.     case 1:
  470.       if (!PyArg_Parse(args, "O;ch or int", &temp))
  471.         return NULL;
  472.       break;
  473.     case 2:
  474.       if (!PyArg_Parse(args,"(Ol);ch or int,attr", &temp, &attr))
  475.         return NULL;
  476.       break;
  477.     default:
  478.       PyErr_SetString(PyExc_TypeError, "bkgdset requires 1 or 2 arguments");
  479.       return NULL;
  480.   }
  481.  
  482.   if (!PyCurses_ConvertToChtype(temp, &bkgd)) {
  483.     PyErr_SetString(PyExc_TypeError, "argument 1 must be a ch or an int");
  484.     return NULL;
  485.   }
  486.  
  487.   wbkgdset(self->win, bkgd | attr);
  488.   return PyCursesCheckERR(0, "bkgdset");
  489. }
  490.  
  491. static PyObject *
  492. PyCursesWindow_Border(PyCursesWindowObject *self, PyObject *args)
  493. {
  494.   chtype ls, rs, ts, bs, tl, tr, bl, br;
  495.   ls = rs = ts = bs = tl = tr = bl = br = 0;
  496.   if (!PyArg_Parse(args,"|llllllll;ls,rs,ts,bs,tl,tr,bl,br",
  497.                         &ls, &rs, &ts, &bs, &tl, &tr, &bl, &br))
  498.     return NULL;
  499.   wborder(self->win, ls, rs, ts, bs, tl, tr, bl, br);
  500.   Py_INCREF(Py_None);
  501.   return Py_None;
  502. }
  503.  
  504. static PyObject *
  505. PyCursesWindow_Box(PyCursesWindowObject *self, PyObject *args)
  506. {
  507.   chtype ch1=0,ch2=0;
  508.   if (!PyArg_NoArgs(args)) {
  509.     PyErr_Clear();
  510.     if (!PyArg_Parse(args,"(ll);vertint,horint", &ch1, &ch2))
  511.       return NULL;
  512.   }
  513.   box(self->win,ch1,ch2);
  514.   Py_INCREF(Py_None);
  515.   return Py_None;
  516. }
  517.  
  518. static PyObject *
  519. PyCursesWindow_DelCh(PyCursesWindowObject *self, PyObject *args)
  520. {
  521.   int rtn;
  522.   int x, y;
  523.  
  524.   switch (ARG_COUNT(args)) {
  525.   case 0:
  526.     rtn = wdelch(self->win);
  527.     break;
  528.   case 2:
  529.     if (!PyArg_Parse(args,"(ii);y,x", &y, &x))
  530.       return NULL;
  531.     rtn = mvwdelch(self->win,y,x);
  532.     break;
  533.   default:
  534.     PyErr_SetString(PyExc_TypeError, "delch requires 0 or 2 arguments");
  535.     return NULL;
  536.   }
  537.   return PyCursesCheckERR(rtn, "[mv]wdelch");
  538. }
  539.  
  540. static PyObject *
  541. PyCursesWindow_DerWin(PyCursesWindowObject *self, PyObject *args)
  542. {
  543.   WINDOW *win;
  544.   int nlines, ncols, begin_y, begin_x;
  545.  
  546.   nlines = 0;
  547.   ncols  = 0;
  548.   switch (ARG_COUNT(args)) {
  549.   case 2:
  550.     if (!PyArg_Parse(args,"(ii);begin_y,begin_x",&begin_y,&begin_x))
  551.       return NULL;
  552.     break;
  553.   case 4:
  554.     if (!PyArg_Parse(args, "(iiii);nlines,ncols,begin_y,begin_x",
  555.            &nlines,&ncols,&begin_y,&begin_x))
  556.       return NULL;
  557.     break;
  558.   default:
  559.     PyErr_SetString(PyExc_TypeError, "derwin requires 2 or 4 arguments");
  560.     return NULL;
  561.   }
  562.  
  563.   win = derwin(self->win,nlines,ncols,begin_y,begin_x);
  564.  
  565.   if (win == NULL) {
  566.     PyErr_SetString(PyCursesError, catchall_NULL);
  567.     return NULL;
  568.   }
  569.  
  570.   return (PyObject *)PyCursesWindow_New(win);
  571. }
  572.  
  573. static PyObject *
  574. PyCursesWindow_EchoChar(PyCursesWindowObject *self, PyObject *args)
  575. {
  576.   PyObject *temp;
  577.   chtype ch;
  578.   attr_t attr = A_NORMAL;
  579.  
  580.   switch (ARG_COUNT(args)) {
  581.   case 1:
  582.     if (!PyArg_Parse(args,"O;ch or int", &temp))
  583.       return NULL;
  584.     break;
  585.   case 2:
  586.     if (!PyArg_Parse(args,"(Ol);ch or int,attr", &temp, &attr))
  587.       return NULL;
  588.     break;
  589.   default:
  590.     PyErr_SetString(PyExc_TypeError, "echochar requires 1 or 2 arguments");
  591.  
  592.  
  593.     return NULL;
  594.   }
  595.  
  596.   if (!PyCurses_ConvertToChtype(temp, &ch)) {
  597.     PyErr_SetString(PyExc_TypeError, "argument 1 must be a ch or an int");
  598.     return NULL;
  599.   }
  600.   
  601.   if (self->win->_flags & _ISPAD)
  602.     return PyCursesCheckERR(pechochar(self->win, ch | attr), 
  603.                 "echochar");
  604.   else
  605.     return PyCursesCheckERR(wechochar(self->win, ch | attr), 
  606.                 "echochar");
  607. }
  608.  
  609. #ifdef NCURSES_MOUSE_VERSION
  610. static PyObject *
  611. PyCursesWindow_Enclose(PyCursesWindowObject *self, PyObject *args)
  612. {
  613.     int x, y;
  614.     if (!PyArg_Parse(args,"(ii);y,x", &y, &x))
  615.         return NULL;
  616.  
  617.     return PyInt_FromLong( wenclose(self->win,y,x) );
  618. }
  619. #endif
  620.  
  621. static PyObject *
  622. PyCursesWindow_GetBkgd(PyCursesWindowObject *self, PyObject *args)
  623. {
  624.   if (!PyArg_NoArgs(args))
  625.     return NULL;
  626.   return PyInt_FromLong((long) getbkgd(self->win));
  627. }
  628.  
  629. static PyObject *
  630. PyCursesWindow_GetCh(PyCursesWindowObject *self, PyObject *args)
  631. {
  632.   int x, y;
  633.   chtype rtn;
  634.  
  635.   switch (ARG_COUNT(args)) {
  636.   case 0:
  637.     Py_BEGIN_ALLOW_THREADS
  638.     rtn = wgetch(self->win);
  639.     Py_END_ALLOW_THREADS
  640.     break;
  641.   case 2:
  642.     if (!PyArg_Parse(args,"(ii);y,x",&y,&x))
  643.       return NULL;
  644.     Py_BEGIN_ALLOW_THREADS
  645.     rtn = mvwgetch(self->win,y,x);
  646.     Py_END_ALLOW_THREADS
  647.     break;
  648.   default:
  649.     PyErr_SetString(PyExc_TypeError, "getch requires 0 or 2 arguments");
  650.     return NULL;
  651.   }
  652.   return PyInt_FromLong(rtn);
  653. }
  654.  
  655. static PyObject *
  656. PyCursesWindow_GetKey(PyCursesWindowObject *self, PyObject *args)
  657. {
  658.   int x, y;
  659.   chtype rtn;
  660.  
  661.   switch (ARG_COUNT(args)) {
  662.   case 0:
  663.     Py_BEGIN_ALLOW_THREADS
  664.     rtn = wgetch(self->win);
  665.     Py_END_ALLOW_THREADS
  666.     break;
  667.   case 2:
  668.     if (!PyArg_Parse(args,"(ii);y,x",&y,&x))
  669.       return NULL;
  670.     Py_BEGIN_ALLOW_THREADS
  671.     rtn = mvwgetch(self->win,y,x);
  672.     Py_END_ALLOW_THREADS
  673.     break;
  674.   default:
  675.     PyErr_SetString(PyExc_TypeError, "getkey requires 0 or 2 arguments");
  676.     return NULL;
  677.   }
  678.   if (rtn<=255)
  679.     return Py_BuildValue("c", rtn);
  680.   else
  681.     return PyString_FromString((char *)keyname(rtn));
  682. }
  683.  
  684. static PyObject *
  685. PyCursesWindow_GetStr(PyCursesWindowObject *self, PyObject *args)
  686. {
  687.   int x, y, n;
  688.   char rtn[1024]; /* This should be big enough.. I hope */
  689.   int rtn2;
  690.  
  691.   switch (ARG_COUNT(args)) {
  692.   case 0:
  693.     Py_BEGIN_ALLOW_THREADS
  694.     rtn2 = wgetstr(self->win,rtn);
  695.     Py_END_ALLOW_THREADS
  696.     break;
  697.   case 1:
  698.     if (!PyArg_Parse(args,"i;n", &n))
  699.       return NULL;
  700.     Py_BEGIN_ALLOW_THREADS
  701.     rtn2 = wgetnstr(self->win,rtn,n);
  702.     Py_END_ALLOW_THREADS
  703.     break;
  704.   case 2:
  705.     if (!PyArg_Parse(args,"(ii);y,x",&y,&x))
  706.       return NULL;
  707.     Py_BEGIN_ALLOW_THREADS
  708.     rtn2 = mvwgetstr(self->win,y,x,rtn);
  709.     Py_END_ALLOW_THREADS
  710.     break;
  711.   case 3:
  712.     if (!PyArg_Parse(args,"(iii);y,x,n", &y, &x, &n))
  713.       return NULL;
  714. #ifdef STRICT_SYSV_CURSES
  715.  /* Untested */
  716.     Py_BEGIN_ALLOW_THREADS
  717.     rtn2 = wmove(self->win,y,x)==ERR ? ERR :
  718.       wgetnstr(self->win, rtn, n);
  719.     Py_END_ALLOW_THREADS
  720. #else
  721.     Py_BEGIN_ALLOW_THREADS
  722.     rtn2 = mvwgetnstr(self->win, y, x, rtn, n);
  723.     Py_END_ALLOW_THREADS
  724. #endif
  725.     break;
  726.   default:
  727.     PyErr_SetString(PyExc_TypeError, "getstr requires 0 to 2 arguments");
  728.     return NULL;
  729.   }
  730.   if (rtn2 == ERR)
  731.     rtn[0] = 0;
  732.   return PyString_FromString(rtn);
  733. }
  734.  
  735. static PyObject *
  736. PyCursesWindow_Hline(PyCursesWindowObject *self, PyObject *args)
  737. {
  738.   PyObject *temp;
  739.   chtype ch;
  740.   int n, x, y, code = OK;
  741.   attr_t attr = A_NORMAL;
  742.  
  743.   switch (ARG_COUNT(args)) {
  744.   case 2:
  745.     if (!PyArg_Parse(args, "(Oi);ch or int,n", &temp, &n))
  746.       return NULL;
  747.     break;
  748.   case 3:
  749.     if (!PyArg_Parse(args, "(Oil);ch or int,n,attr", &temp, &n, &attr))
  750.       return NULL;
  751.     break;
  752.   case 4:
  753.     if (!PyArg_Parse(args, "(iiOi);y,x,ch or int,n", &y, &x, &temp, &n))
  754.       return NULL;
  755.     code = wmove(self->win, y, x);
  756.     break;
  757.   case 5:
  758.     if (!PyArg_Parse(args, "(iiOil); y,x,ch or int,n,attr", 
  759.              &y, &x, &temp, &n, &attr))
  760.       return NULL;
  761.     code = wmove(self->win, y, x);
  762.   default:
  763.     PyErr_SetString(PyExc_TypeError, "hline requires 2 or 5 arguments");
  764.     return NULL;
  765.   }
  766.  
  767.   if (code != ERR) {
  768.     if (!PyCurses_ConvertToChtype(temp, &ch)) {
  769.       PyErr_SetString(PyExc_TypeError, 
  770.               "argument 1 or 3 must be a ch or an int");
  771.       return NULL;
  772.     }
  773.     return PyCursesCheckERR(whline(self->win, ch | attr, n), "hline");
  774.   } else 
  775.     return PyCursesCheckERR(code, "wmove");
  776. }
  777.  
  778. static PyObject *
  779. PyCursesWindow_InsCh(PyCursesWindowObject *self, PyObject *args)
  780. {
  781.   int rtn, x, y, use_xy = FALSE;
  782.   PyObject *temp;
  783.   chtype ch = 0;
  784.   attr_t attr = A_NORMAL;
  785.   
  786.   switch (ARG_COUNT(args)) {
  787.   case 1:
  788.     if (!PyArg_Parse(args, "O;ch or int", &temp))
  789.       return NULL;
  790.     break;
  791.   case 2:
  792.     if (!PyArg_Parse(args, "(Ol);ch or int,attr", &temp, &attr))
  793.       return NULL;
  794.     break;
  795.   case 3:
  796.     if (!PyArg_Parse(args,"(iiO);y,x,ch or int", &y, &x, &temp))
  797.       return NULL;
  798.     use_xy = TRUE;
  799.     break;
  800.   case 4:
  801.     if (!PyArg_Parse(args,"(iiOl);y,x,ch or int, attr", &y, &x, &temp, &attr))
  802.       return NULL;
  803.     use_xy = TRUE;
  804.     break;
  805.   default:
  806.     PyErr_SetString(PyExc_TypeError, "insch requires 1 or 4 arguments");
  807.     return NULL;
  808.   }
  809.  
  810.   if (!PyCurses_ConvertToChtype(temp, &ch)) {
  811.     PyErr_SetString(PyExc_TypeError, 
  812.             "argument 1 or 3 must be a ch or an int");
  813.     return NULL;
  814.   }
  815.   
  816.   if (use_xy == TRUE)
  817.     rtn = mvwinsch(self->win,y,x, ch | attr);
  818.   else {
  819.     rtn = winsch(self->win, ch | attr);
  820.   }
  821.   return PyCursesCheckERR(rtn, "insch");
  822. }
  823.  
  824. static PyObject *
  825. PyCursesWindow_InCh(PyCursesWindowObject *self, PyObject *args)
  826. {
  827.   int x, y, rtn;
  828.  
  829.   switch (ARG_COUNT(args)) {
  830.   case 0:
  831.     rtn = winch(self->win);
  832.     break;
  833.   case 2:
  834.     if (!PyArg_Parse(args,"(ii);y,x",&y,&x))
  835.       return NULL;
  836.     rtn = mvwinch(self->win,y,x);
  837.     break;
  838.   default:
  839.     PyErr_SetString(PyExc_TypeError, "inch requires 0 or 2 arguments");
  840.     return NULL;
  841.   }
  842.   return PyInt_FromLong((long) rtn);
  843. }
  844.  
  845. static PyObject *
  846. PyCursesWindow_InStr(PyCursesWindowObject *self, PyObject *args)
  847. {
  848.   int x, y, n;
  849.   char rtn[1024]; /* This should be big enough.. I hope */
  850.   int rtn2;
  851.  
  852.   switch (ARG_COUNT(args)) {
  853.   case 0:
  854.     rtn2 = winstr(self->win,rtn);
  855.     break;
  856.   case 1:
  857.     if (!PyArg_Parse(args,"i;n", &n))
  858.       return NULL;
  859.     rtn2 = winnstr(self->win,rtn,n);
  860.     break;
  861.   case 2:
  862.     if (!PyArg_Parse(args,"(ii);y,x",&y,&x))
  863.       return NULL;
  864.     rtn2 = mvwinstr(self->win,y,x,rtn);
  865.     break;
  866.   case 3:
  867.     if (!PyArg_Parse(args, "(iii);y,x,n", &y, &x, &n))
  868.       return NULL;
  869.     rtn2 = mvwinnstr(self->win, y, x, rtn, n);
  870.     break;
  871.   default:
  872.     PyErr_SetString(PyExc_TypeError, "instr requires 0 or 3 arguments");
  873.     return NULL;
  874.   }
  875.   if (rtn2 == ERR)
  876.     rtn[0] = 0;
  877.   return PyString_FromString(rtn);
  878. }
  879.  
  880. static PyObject *
  881. PyCursesWindow_InsStr(PyCursesWindowObject *self, PyObject *args)
  882. {
  883.   int rtn;
  884.   int x, y;
  885.   char *str;
  886.   attr_t attr = A_NORMAL , attr_old = A_NORMAL;
  887.   int use_xy = FALSE, use_attr = FALSE;
  888.  
  889.   switch (ARG_COUNT(args)) {
  890.   case 1:
  891.     if (!PyArg_Parse(args,"s;str", &str))
  892.       return NULL;
  893.     break;
  894.   case 2:
  895.     if (!PyArg_Parse(args,"(sl);str,attr", &str, &attr))
  896.       return NULL;
  897.     use_attr = TRUE;
  898.     break;
  899.   case 3:
  900.     if (!PyArg_Parse(args,"(iis);y,x,str", &y, &x, &str))
  901.       return NULL;
  902.     use_xy = TRUE;
  903.     break;
  904.   case 4:
  905.     if (!PyArg_Parse(args,"(iisl);y,x,str,attr", &y, &x, &str, &attr))
  906.       return NULL;
  907.     use_xy = use_attr = TRUE;
  908.     break;
  909.   default:
  910.     PyErr_SetString(PyExc_TypeError, "insstr requires 1 to 4 arguments");
  911.     return NULL;
  912.   }
  913.  
  914.   if (use_attr == TRUE) {
  915.     attr_old = getattrs(self->win);
  916.     wattrset(self->win,attr);
  917.   }
  918.   if (use_xy == TRUE)
  919.     rtn = mvwinsstr(self->win,y,x,str);
  920.   else
  921.     rtn = winsstr(self->win,str);
  922.   if (use_attr == TRUE)
  923.     wattrset(self->win,attr_old);
  924.   return PyCursesCheckERR(rtn, "insstr");
  925. }
  926.  
  927. static PyObject *
  928. PyCursesWindow_InsNStr(PyCursesWindowObject *self, PyObject *args)
  929. {
  930.   int rtn, x, y, n;
  931.   char *str;
  932.   attr_t attr = A_NORMAL , attr_old = A_NORMAL;
  933.   int use_xy = FALSE, use_attr = FALSE;
  934.  
  935.   switch (ARG_COUNT(args)) {
  936.   case 2:
  937.     if (!PyArg_Parse(args,"(si);str,n", &str, &n))
  938.       return NULL;
  939.     break;
  940.   case 3:
  941.     if (!PyArg_Parse(args,"(sil);str,n,attr", &str, &n, &attr))
  942.       return NULL;
  943.     use_attr = TRUE;
  944.     break;
  945.   case 4:
  946.     if (!PyArg_Parse(args,"(iisi);y,x,str,n", &y, &x, &str, &n))
  947.       return NULL;
  948.     use_xy = TRUE;
  949.     break;
  950.   case 5:
  951.     if (!PyArg_Parse(args,"(iisil);y,x,str,n,attr", &y, &x, &str, &n, &attr))
  952.       return NULL;
  953.     use_xy = use_attr = TRUE;
  954.     break;
  955.   default:
  956.     PyErr_SetString(PyExc_TypeError, "insnstr requires 2 to 5 arguments");
  957.     return NULL;
  958.   }
  959.  
  960.   if (use_attr == TRUE) {
  961.     attr_old = getattrs(self->win);
  962.     wattrset(self->win,attr);
  963.   }
  964.   if (use_xy == TRUE)
  965.     rtn = mvwinsnstr(self->win,y,x,str,n);
  966.   else
  967.     rtn = winsnstr(self->win,str,n);
  968.   if (use_attr == TRUE)
  969.     wattrset(self->win,attr_old);
  970.   return PyCursesCheckERR(rtn, "insnstr");
  971. }
  972.  
  973. static PyObject *
  974. PyCursesWindow_Is_LineTouched(PyCursesWindowObject *self, PyObject *args)
  975. {
  976.   int line, erg;
  977.   if (!PyArg_Parse(args,"i;line", &line))
  978.     return NULL;
  979.   erg = is_linetouched(self->win, line);
  980.   if (erg == ERR) {
  981.     PyErr_SetString(PyExc_TypeError, 
  982.             "is_linetouched: line number outside of boundaries");
  983.     return NULL;
  984.   } else 
  985.     if (erg == FALSE) {
  986.       Py_INCREF(Py_False);
  987.       return Py_False;
  988.     } else {
  989.       Py_INCREF(Py_True);
  990.       return Py_True;
  991.     }
  992. }
  993.  
  994. static PyObject *
  995. PyCursesWindow_NoOutRefresh(PyCursesWindowObject *self, PyObject *args)
  996. {
  997.   int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol;
  998.   int rtn;
  999.  
  1000.   if (self->win->_flags & _ISPAD) {
  1001.     switch(ARG_COUNT(args)) {
  1002.     case 6:
  1003.       if (!PyArg_Parse(args, 
  1004.                "(iiiiii);" \
  1005.                "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol", 
  1006.                &pminrow, &pmincol, &sminrow, 
  1007.                &smincol, &smaxrow, &smaxcol))
  1008.     return NULL;
  1009.       Py_BEGIN_ALLOW_THREADS
  1010.       rtn = pnoutrefresh(self->win,
  1011.              pminrow, pmincol, sminrow, 
  1012.              smincol, smaxrow, smaxcol);
  1013.       Py_END_ALLOW_THREADS
  1014.       return PyCursesCheckERR(rtn, "pnoutrefresh");
  1015.     default:
  1016.       PyErr_SetString(PyCursesError, 
  1017.               "noutrefresh() called for a pad " 
  1018.               "requires 6 arguments");
  1019.       return NULL;
  1020.     }
  1021.   } else {
  1022.     if (!PyArg_NoArgs(args))
  1023.       return NULL;    
  1024.  
  1025.     Py_BEGIN_ALLOW_THREADS
  1026.     rtn = wnoutrefresh(self->win);
  1027.     Py_END_ALLOW_THREADS
  1028.     return PyCursesCheckERR(rtn, "wnoutrefresh");
  1029.   }
  1030. }
  1031.  
  1032. static PyObject *
  1033. PyCursesWindow_PutWin(PyCursesWindowObject *self, PyObject *args)
  1034. {
  1035.   PyObject *temp;
  1036.   
  1037.   if (!PyArg_Parse(args, "O;fileobj", &temp))
  1038.     return NULL;
  1039.   if (!PyFile_Check(temp)) {
  1040.     PyErr_SetString(PyExc_TypeError, "argument must be a file object");
  1041.     return NULL;
  1042.   }
  1043.   return PyCursesCheckERR(putwin(self->win, PyFile_AsFile(temp)), 
  1044.               "putwin");
  1045. }
  1046.  
  1047. static PyObject *
  1048. PyCursesWindow_RedrawLine(PyCursesWindowObject *self, PyObject *args)
  1049. {
  1050.   int beg, num;
  1051.   if (!PyArg_Parse(args,"(ii);beg,num", &beg, &num))
  1052.     return NULL;
  1053.   return PyCursesCheckERR(wredrawln(self->win,beg,num), "redrawln");
  1054. }
  1055.  
  1056. static PyObject *
  1057. PyCursesWindow_Refresh(PyCursesWindowObject *self, PyObject *args)
  1058. {
  1059.   int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol;
  1060.   int rtn;
  1061.   
  1062.   if (self->win->_flags & _ISPAD) {
  1063.     switch(ARG_COUNT(args)) {
  1064.     case 6:
  1065.       if (!PyArg_Parse(args, 
  1066.                "(iiiiii);" \
  1067.                "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol", 
  1068.                &pminrow, &pmincol, &sminrow, 
  1069.                &smincol, &smaxrow, &smaxcol))
  1070.     return NULL;
  1071.  
  1072.       Py_BEGIN_ALLOW_THREADS
  1073.       rtn = prefresh(self->win,
  1074.              pminrow, pmincol, sminrow, 
  1075.              smincol, smaxrow, smaxcol);
  1076.       Py_END_ALLOW_THREADS
  1077.       return PyCursesCheckERR(rtn, "prefresh");
  1078.     default:
  1079.       PyErr_SetString(PyCursesError, 
  1080.               "refresh() for a pad requires 6 arguments");
  1081.       return NULL;
  1082.     }
  1083.   } else {
  1084.     if (!PyArg_NoArgs(args))
  1085.       return NULL;    
  1086.     Py_BEGIN_ALLOW_THREADS
  1087.     rtn = wrefresh(self->win);
  1088.     Py_END_ALLOW_THREADS
  1089.     return PyCursesCheckERR(rtn, "prefresh");    
  1090.   }
  1091. }
  1092.  
  1093. static PyObject *
  1094. PyCursesWindow_SetScrollRegion(PyCursesWindowObject *self, PyObject *args)
  1095. {
  1096.   int x, y;
  1097.   if (!PyArg_Parse(args,"(ii);top, bottom",&y,&x))
  1098.     return NULL;
  1099.   return PyCursesCheckERR(wsetscrreg(self->win,y,x), "wsetscrreg");
  1100. }
  1101.  
  1102. static PyObject *
  1103. PyCursesWindow_SubWin(PyCursesWindowObject *self, PyObject *args)
  1104. {
  1105.   WINDOW *win;
  1106.   int nlines, ncols, begin_y, begin_x;
  1107.  
  1108.   nlines = 0;
  1109.   ncols  = 0;
  1110.   switch (ARG_COUNT(args)) {
  1111.   case 2:
  1112.     if (!PyArg_Parse(args,"(ii);begin_y,begin_x",&begin_y,&begin_x))
  1113.       return NULL;
  1114.     break;
  1115.   case 4:
  1116.     if (!PyArg_Parse(args, "(iiii);nlines,ncols,begin_y,begin_x",
  1117.            &nlines,&ncols,&begin_y,&begin_x))
  1118.       return NULL;
  1119.     break;
  1120.   default:
  1121.     PyErr_SetString(PyExc_TypeError, "subwin requires 2 or 4 arguments");
  1122.     return NULL;
  1123.   }
  1124.  
  1125.   /* printf("Subwin: %i %i %i %i   \n", nlines, ncols, begin_y, begin_x); */
  1126.   if (self->win->_flags & _ISPAD)
  1127.     win = subpad(self->win, nlines, ncols, begin_y, begin_x);
  1128.   else
  1129.     win = subwin(self->win, nlines, ncols, begin_y, begin_x);
  1130.  
  1131.   if (win == NULL) {
  1132.     PyErr_SetString(PyCursesError, catchall_NULL);
  1133.     return NULL;
  1134.   }
  1135.   
  1136.   return (PyObject *)PyCursesWindow_New(win);
  1137. }
  1138.  
  1139. static PyObject *
  1140. PyCursesWindow_Scroll(PyCursesWindowObject *self, PyObject *args)
  1141. {
  1142.   int lines;
  1143.   switch(ARG_COUNT(args)) {
  1144.   case 0:
  1145.     return PyCursesCheckERR(scroll(self->win), "scroll");
  1146.     break;
  1147.   case 1:
  1148.     if (!PyArg_Parse(args, "i;lines", &lines))
  1149.       return NULL;
  1150.     return PyCursesCheckERR(wscrl(self->win, lines), "scroll");
  1151.   default:
  1152.     PyErr_SetString(PyExc_TypeError, "scroll requires 0 or 1 arguments");
  1153.     return NULL;
  1154.   }
  1155. }
  1156.  
  1157. static PyObject *
  1158. PyCursesWindow_TouchLine(PyCursesWindowObject *self, PyObject *args)
  1159. {
  1160.   int st, cnt, val;
  1161.   switch (ARG_COUNT(args)) {
  1162.   case 2:
  1163.     if (!PyArg_Parse(args,"(ii);start,count",&st,&cnt))
  1164.       return NULL;
  1165.     return PyCursesCheckERR(touchline(self->win,st,cnt), "touchline");
  1166.     break;
  1167.   case 3:
  1168.     if (!PyArg_Parse(args, "(iii);start,count,val", &st, &cnt, &val))
  1169.       return NULL;
  1170.     return PyCursesCheckERR(wtouchln(self->win, st, cnt, val), "touchline");
  1171.   default:
  1172.     PyErr_SetString(PyExc_TypeError, "touchline requires 2 or 3 arguments");
  1173.     return NULL;
  1174.   }
  1175. }
  1176.  
  1177. static PyObject *
  1178. PyCursesWindow_Vline(PyCursesWindowObject *self, PyObject *args)
  1179. {
  1180.   PyObject *temp;
  1181.   chtype ch;
  1182.   int n, x, y, code = OK;
  1183.   attr_t attr = A_NORMAL;
  1184.  
  1185.   switch (ARG_COUNT(args)) {
  1186.   case 2:
  1187.     if (!PyArg_Parse(args, "(Oi);ch or int,n", &temp, &n))
  1188.       return NULL;
  1189.     break;
  1190.   case 3:
  1191.     if (!PyArg_Parse(args, "(Oil);ch or int,n,attr", &temp, &n, &attr))
  1192.       return NULL;
  1193.     break;
  1194.   case 4:
  1195.     if (!PyArg_Parse(args, "(iiOi);y,x,ch or int,n", &y, &x, &temp, &n))
  1196.       return NULL;
  1197.     code = wmove(self->win, y, x);
  1198.     break;
  1199.   case 5:
  1200.     if (!PyArg_Parse(args, "(iiOil); y,x,ch or int,n,attr", 
  1201.              &y, &x, &temp, &n, &attr))
  1202.       return NULL;
  1203.     code = wmove(self->win, y, x);
  1204.   default:
  1205.     PyErr_SetString(PyExc_TypeError, "vline requires 2 or 5 arguments");
  1206.     return NULL;
  1207.   }
  1208.  
  1209.   if (code != ERR) {
  1210.     if (!PyCurses_ConvertToChtype(temp, &ch)) {
  1211.       PyErr_SetString(PyExc_TypeError, 
  1212.               "argument 1 or 3 must be a ch or an int");
  1213.       return NULL;
  1214.     }
  1215.     return PyCursesCheckERR(wvline(self->win, ch | attr, n), "vline");
  1216.   } else
  1217.     return PyCursesCheckERR(code, "wmove");
  1218. }
  1219.  
  1220. static PyMethodDef PyCursesWindow_Methods[] = {
  1221.     {"addch",           (PyCFunction)PyCursesWindow_AddCh},
  1222.     {"addnstr",         (PyCFunction)PyCursesWindow_AddNStr},
  1223.     {"addstr",          (PyCFunction)PyCursesWindow_AddStr},
  1224.     {"attroff",         (PyCFunction)PyCursesWindow_wattroff},
  1225.     {"attron",          (PyCFunction)PyCursesWindow_wattron},
  1226.     {"attrset",         (PyCFunction)PyCursesWindow_wattrset},
  1227.     {"bkgd",            (PyCFunction)PyCursesWindow_Bkgd},
  1228.     {"bkgdset",         (PyCFunction)PyCursesWindow_BkgdSet},
  1229.     {"border",          (PyCFunction)PyCursesWindow_Border, METH_VARARGS},
  1230.     {"box",             (PyCFunction)PyCursesWindow_Box},
  1231.     {"clear",           (PyCFunction)PyCursesWindow_wclear},
  1232.     {"clearok",         (PyCFunction)PyCursesWindow_clearok},
  1233.     {"clrtobot",        (PyCFunction)PyCursesWindow_wclrtobot},
  1234.     {"clrtoeol",        (PyCFunction)PyCursesWindow_wclrtoeol},
  1235.     {"cursyncup",       (PyCFunction)PyCursesWindow_wcursyncup},
  1236.     {"delch",           (PyCFunction)PyCursesWindow_DelCh},
  1237.     {"deleteln",        (PyCFunction)PyCursesWindow_wdeleteln},
  1238.     {"derwin",          (PyCFunction)PyCursesWindow_DerWin},
  1239.     {"echochar",        (PyCFunction)PyCursesWindow_EchoChar},
  1240. #ifdef NCURSES_MOUSE_VERSION
  1241.     {"enclose",         (PyCFunction)PyCursesWindow_Enclose},
  1242. #endif
  1243.     {"erase",           (PyCFunction)PyCursesWindow_werase},
  1244.     {"getbegyx",        (PyCFunction)PyCursesWindow_getbegyx},
  1245.     {"getbkgd",         (PyCFunction)PyCursesWindow_GetBkgd},
  1246.     {"getch",           (PyCFunction)PyCursesWindow_GetCh},
  1247.     {"getkey",          (PyCFunction)PyCursesWindow_GetKey},
  1248.     {"getmaxyx",        (PyCFunction)PyCursesWindow_getmaxyx},
  1249.     {"getparyx",        (PyCFunction)PyCursesWindow_getparyx},
  1250.     {"getstr",          (PyCFunction)PyCursesWindow_GetStr},
  1251.     {"getyx",           (PyCFunction)PyCursesWindow_getyx},
  1252.     {"hline",           (PyCFunction)PyCursesWindow_Hline},
  1253.     {"idcok",           (PyCFunction)PyCursesWindow_idcok},
  1254.     {"idlok",           (PyCFunction)PyCursesWindow_idlok},
  1255.     {"immedok",         (PyCFunction)PyCursesWindow_immedok},
  1256.     {"inch",            (PyCFunction)PyCursesWindow_InCh},
  1257.     {"insch",           (PyCFunction)PyCursesWindow_InsCh},
  1258.     {"insdelln",        (PyCFunction)PyCursesWindow_winsdelln},
  1259.     {"insertln",        (PyCFunction)PyCursesWindow_winsertln},
  1260.     {"insnstr",         (PyCFunction)PyCursesWindow_InsNStr},
  1261.     {"insstr",          (PyCFunction)PyCursesWindow_InsStr},
  1262.     {"instr",           (PyCFunction)PyCursesWindow_InStr},
  1263.     {"is_linetouched",  (PyCFunction)PyCursesWindow_Is_LineTouched},
  1264.     {"is_wintouched",   (PyCFunction)PyCursesWindow_is_wintouched},
  1265.     {"keypad",          (PyCFunction)PyCursesWindow_keypad},
  1266.     {"leaveok",         (PyCFunction)PyCursesWindow_leaveok},
  1267.     {"move",            (PyCFunction)PyCursesWindow_wmove},
  1268.     {"mvderwin",        (PyCFunction)PyCursesWindow_mvderwin},
  1269.     {"mvwin",           (PyCFunction)PyCursesWindow_mvwin},
  1270.     {"nodelay",         (PyCFunction)PyCursesWindow_nodelay},
  1271.     {"notimeout",       (PyCFunction)PyCursesWindow_notimeout},
  1272.     {"noutrefresh",     (PyCFunction)PyCursesWindow_NoOutRefresh},
  1273.     {"putwin",          (PyCFunction)PyCursesWindow_PutWin},
  1274.     {"redrawln",        (PyCFunction)PyCursesWindow_RedrawLine},
  1275.     {"redrawwin",       (PyCFunction)PyCursesWindow_redrawwin},
  1276.     {"refresh",         (PyCFunction)PyCursesWindow_Refresh},
  1277. #ifndef STRICT_SYSV_CURSES
  1278.     {"resize",          (PyCFunction)PyCursesWindow_wresize},
  1279. #endif
  1280.     {"scroll",          (PyCFunction)PyCursesWindow_Scroll},
  1281.     {"scrollok",        (PyCFunction)PyCursesWindow_scrollok},
  1282.     {"setscrreg",       (PyCFunction)PyCursesWindow_SetScrollRegion},
  1283.     {"standend",        (PyCFunction)PyCursesWindow_wstandend},
  1284.     {"standout",        (PyCFunction)PyCursesWindow_wstandout},
  1285.     {"subpad",          (PyCFunction)PyCursesWindow_SubWin},
  1286.     {"subwin",          (PyCFunction)PyCursesWindow_SubWin},
  1287.     {"syncdown",        (PyCFunction)PyCursesWindow_wsyncdown},
  1288.     {"syncok",          (PyCFunction)PyCursesWindow_syncok},
  1289.     {"syncup",          (PyCFunction)PyCursesWindow_wsyncup},
  1290.     {"timeout",         (PyCFunction)PyCursesWindow_wtimeout},
  1291.     {"touchline",       (PyCFunction)PyCursesWindow_TouchLine},
  1292.     {"touchwin",        (PyCFunction)PyCursesWindow_touchwin},
  1293.     {"untouchwin",      (PyCFunction)PyCursesWindow_untouchwin},
  1294.     {"vline",           (PyCFunction)PyCursesWindow_Vline},
  1295.     {NULL,                NULL}   /* sentinel */
  1296. };
  1297.  
  1298. static PyObject *
  1299. PyCursesWindow_GetAttr(PyCursesWindowObject *self, char *name)
  1300. {
  1301.   return Py_FindMethod(PyCursesWindow_Methods, (PyObject *)self, name);
  1302. }
  1303.  
  1304. /* -------------------------------------------------------*/
  1305.  
  1306. PyTypeObject PyCursesWindow_Type = {
  1307.     PyObject_HEAD_INIT(&PyType_Type)
  1308.     0,            /*ob_size*/
  1309.     "curses window",    /*tp_name*/
  1310.     sizeof(PyCursesWindowObject),    /*tp_basicsize*/
  1311.     0,            /*tp_itemsize*/
  1312.     /* methods */
  1313.     (destructor)PyCursesWindow_Dealloc, /*tp_dealloc*/
  1314.     0,            /*tp_print*/
  1315.     (getattrfunc)PyCursesWindow_GetAttr, /*tp_getattr*/
  1316.     (setattrfunc)0, /*tp_setattr*/
  1317.     0,            /*tp_compare*/
  1318.     0,            /*tp_repr*/
  1319.     0,            /*tp_as_number*/
  1320.     0,            /*tp_as_sequence*/
  1321.     0,            /*tp_as_mapping*/
  1322.     0,            /*tp_hash*/
  1323. };
  1324.  
  1325. /*********************************************************************
  1326.  Global Functions
  1327. **********************************************************************/
  1328.  
  1329. static PyObject *ModDict;
  1330.  
  1331. /* Function Prototype Macros - They are ugly but very, very useful. ;-)
  1332.  
  1333.    X - function name
  1334.    TYPE - parameter Type
  1335.    ERGSTR - format string for construction of the return value
  1336.    PARSESTR - format string for argument parsing
  1337.    */
  1338.  
  1339. #define NoArgNoReturnFunction(X) \
  1340. static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \
  1341. { \
  1342.   PyCursesInitialised \
  1343.   if (!PyArg_NoArgs(args)) return NULL; \
  1344.   return PyCursesCheckERR(X(), # X); }
  1345.  
  1346. #define NoArgOrFlagNoReturnFunction(X) \
  1347. static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \
  1348. { \
  1349.   int flag = 0; \
  1350.   PyCursesInitialised \
  1351.   switch(ARG_COUNT(args)) { \
  1352.   case 0: \
  1353.     return PyCursesCheckERR(X(), # X); \
  1354.   case 1: \
  1355.     if (!PyArg_Parse(args, "i;True(1) or False(0)", &flag)) return NULL; \
  1356.     if (flag) return PyCursesCheckERR(X(), # X); \
  1357.     else return PyCursesCheckERR(no ## X (), # X); \
  1358.   default: \
  1359.     PyErr_SetString(PyExc_TypeError, # X " requires 0 or 1 arguments"); \
  1360.     return NULL; } }
  1361.  
  1362. #define NoArgReturnIntFunction(X) \
  1363. static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \
  1364. { \
  1365.  PyCursesInitialised \
  1366.  if (!PyArg_NoArgs(args)) return NULL; \
  1367.  return PyInt_FromLong((long) X()); }
  1368.  
  1369.  
  1370. #define NoArgReturnStringFunction(X) \
  1371. static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \
  1372. { \
  1373.   PyCursesInitialised \
  1374.   if (!PyArg_NoArgs(args)) return NULL; \
  1375.   return PyString_FromString(X()); }
  1376.  
  1377. #define NoArgTrueFalseFunction(X) \
  1378. static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \
  1379. { \
  1380.   PyCursesInitialised \
  1381.   if (!PyArg_NoArgs(args)) return NULL; \
  1382.   if (X () == FALSE) { \
  1383.     Py_INCREF(Py_False); \
  1384.     return Py_False; \
  1385.   } \
  1386.   Py_INCREF(Py_True); \
  1387.   return Py_True; }
  1388.  
  1389. #define NoArgNoReturnVoidFunction(X) \
  1390. static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \
  1391. { \
  1392.   PyCursesInitialised \
  1393.   if (!PyArg_NoArgs(args)) return NULL; \
  1394.   X(); \
  1395.   Py_INCREF(Py_None); \
  1396.   return Py_None; }
  1397.  
  1398. NoArgNoReturnFunction(beep)
  1399. NoArgNoReturnFunction(def_prog_mode)
  1400. NoArgNoReturnFunction(def_shell_mode)
  1401. NoArgNoReturnFunction(doupdate)
  1402. NoArgNoReturnFunction(endwin)
  1403. NoArgNoReturnFunction(flash)
  1404. NoArgNoReturnFunction(nocbreak)
  1405. NoArgNoReturnFunction(noecho)
  1406. NoArgNoReturnFunction(nonl)
  1407. NoArgNoReturnFunction(noraw)
  1408. NoArgNoReturnFunction(reset_prog_mode)
  1409. NoArgNoReturnFunction(reset_shell_mode)
  1410. NoArgNoReturnFunction(resetty)
  1411. NoArgNoReturnFunction(savetty)
  1412.  
  1413. NoArgOrFlagNoReturnFunction(cbreak)
  1414. NoArgOrFlagNoReturnFunction(echo)
  1415. NoArgOrFlagNoReturnFunction(nl)
  1416. NoArgOrFlagNoReturnFunction(raw)
  1417.  
  1418. NoArgReturnIntFunction(baudrate)
  1419. NoArgReturnIntFunction(termattrs)
  1420.  
  1421. NoArgReturnStringFunction(termname)
  1422. NoArgReturnStringFunction(longname)
  1423.  
  1424. NoArgTrueFalseFunction(can_change_color)
  1425. NoArgTrueFalseFunction(has_colors)
  1426. NoArgTrueFalseFunction(has_ic)
  1427. NoArgTrueFalseFunction(has_il)
  1428. NoArgTrueFalseFunction(isendwin)
  1429. NoArgNoReturnVoidFunction(filter)
  1430. NoArgNoReturnVoidFunction(flushinp)
  1431. NoArgNoReturnVoidFunction(noqiflush)
  1432.  
  1433. static PyObject *
  1434. PyCurses_Color_Content(PyObject *self, PyObject *args)
  1435. {
  1436.   short color,r,g,b;
  1437.  
  1438.   PyCursesInitialised
  1439.   PyCursesInitialisedColor
  1440.  
  1441.   if (ARG_COUNT(args) != 1) {
  1442.     PyErr_SetString(PyExc_TypeError, 
  1443.             "color_content requires 1 argument");
  1444.     return NULL;
  1445.   }
  1446.  
  1447.   if (!PyArg_Parse(args, "h;color", &color)) return NULL;
  1448.  
  1449.   if (color_content(color, &r, &g, &b) != ERR)
  1450.     return Py_BuildValue("(iii)", r, g, b);
  1451.   else {
  1452.     PyErr_SetString(PyCursesError, 
  1453.             "Argument 1 was out of range. Check value of COLORS.");
  1454.     return NULL;
  1455.   }
  1456. }
  1457.  
  1458. static PyObject *
  1459. PyCurses_color_pair(PyObject *self, PyObject *args)
  1460. {
  1461.   int n;
  1462.  
  1463.   PyCursesInitialised
  1464.   PyCursesInitialisedColor
  1465.  
  1466.   if (ARG_COUNT(args) != 1) {
  1467.     PyErr_SetString(PyExc_TypeError, "color_pair requires 1 argument");
  1468.     return NULL;
  1469.   }
  1470.   if (!PyArg_Parse(args, "i;number", &n)) return NULL;
  1471.   return PyInt_FromLong((long) (n << 8));
  1472. }
  1473.  
  1474. static PyObject *
  1475. PyCurses_Curs_Set(PyObject *self, PyObject *args)
  1476. {
  1477.   int vis,erg;
  1478.  
  1479.   PyCursesInitialised
  1480.  
  1481.   if (ARG_COUNT(args)!=1) {
  1482.     PyErr_SetString(PyExc_TypeError, "curs_set requires 1 argument");
  1483.     return NULL;
  1484.   }
  1485.  
  1486.   if (!PyArg_Parse(args, "i;int", &vis)) return NULL;
  1487.  
  1488.   erg = curs_set(vis);
  1489.   if (erg == ERR) return PyCursesCheckERR(erg, "curs_set");
  1490.  
  1491.   return PyInt_FromLong((long) erg);
  1492. }
  1493.  
  1494. static PyObject *
  1495. PyCurses_Delay_Output(PyObject *self, PyObject *args)
  1496. {
  1497.   int ms;
  1498.  
  1499.   PyCursesInitialised
  1500.  
  1501.   if (ARG_COUNT(args) != 1) {
  1502.     PyErr_SetString(PyExc_TypeError, "delay_output requires 1 argument");
  1503.     return NULL;
  1504.   }
  1505.   if (!PyArg_Parse(args, "i;ms", &ms)) return NULL;
  1506.  
  1507.   return PyCursesCheckERR(delay_output(ms), "delay_output");
  1508. }
  1509.  
  1510. static PyObject *
  1511. PyCurses_EraseChar(PyObject *self, PyObject *args)
  1512. {
  1513.   char ch;
  1514.  
  1515.   PyCursesInitialised
  1516.  
  1517.   if (!PyArg_NoArgs(args)) return NULL;
  1518.  
  1519.   ch = erasechar();
  1520.  
  1521.   return PyString_FromString(&ch);
  1522. }
  1523.  
  1524. static PyObject *
  1525. PyCurses_getsyx(PyObject *self, PyObject *args)
  1526. {
  1527.   int x,y;
  1528.  
  1529.   PyCursesInitialised
  1530.  
  1531.   if (!PyArg_NoArgs(args)) return NULL;
  1532.  
  1533.   getsyx(y, x);
  1534.  
  1535.   return Py_BuildValue("(ii)", y, x);
  1536. }
  1537.  
  1538. #ifdef NCURSES_MOUSE_VERSION
  1539. static PyObject *
  1540. PyCurses_GetMouse(PyObject *self, PyObject *args)
  1541. {
  1542.     int rtn;
  1543.     MEVENT event;
  1544.  
  1545.     PyCursesInitialised
  1546.     if (!PyArg_NoArgs(args)) return NULL;
  1547.  
  1548.     rtn = getmouse( &event );
  1549.     if (rtn == ERR) {
  1550.         PyErr_SetString(PyCursesError, "getmouse() returned ERR");
  1551.         return NULL;
  1552.     }
  1553.     return Py_BuildValue("(hiiil)", 
  1554.                  (short)event.id, 
  1555.                  event.x, event.y, event.z,
  1556.                  (long) event.bstate);
  1557. }
  1558.  
  1559. static PyObject *
  1560. PyCurses_UngetMouse(PyObject *self, PyObject *args)
  1561. {
  1562.     MEVENT event;
  1563.  
  1564.     PyCursesInitialised
  1565.     if (!PyArg_ParseTuple(args, "(hiiil)",
  1566.                  &event.id, 
  1567.                  &event.x, &event.y, &event.z,
  1568.                  (int *) &event.bstate))
  1569.       return NULL;
  1570.  
  1571.     return PyCursesCheckERR(ungetmouse(&event), "ungetmouse");
  1572. }
  1573. #endif
  1574.  
  1575. static PyObject *
  1576. PyCurses_GetWin(PyCursesWindowObject *self, PyObject *args)
  1577. {
  1578.   WINDOW *win;
  1579.   PyObject *temp;
  1580.  
  1581.   PyCursesInitialised
  1582.  
  1583.   if (!PyArg_Parse(args, "O;fileobj", &temp)) return NULL;
  1584.  
  1585.   if (!PyFile_Check(temp)) {
  1586.     PyErr_SetString(PyExc_TypeError, "argument must be a file object");
  1587.     return NULL;
  1588.   }
  1589.  
  1590.   win = getwin(PyFile_AsFile(temp));
  1591.  
  1592.   if (win == NULL) {
  1593.     PyErr_SetString(PyCursesError, catchall_NULL);
  1594.     return NULL;
  1595.   }
  1596.  
  1597.   return PyCursesWindow_New(win);
  1598. }
  1599.  
  1600. static PyObject *
  1601. PyCurses_HalfDelay(PyObject *self, PyObject *args)
  1602. {
  1603.   unsigned char tenths;
  1604.  
  1605.   PyCursesInitialised
  1606.  
  1607.   switch(ARG_COUNT(args)) {
  1608.   case 1:
  1609.     if (!PyArg_Parse(args, "b;tenths", &tenths)) return NULL;
  1610.     break;
  1611.   default:
  1612.     PyErr_SetString(PyExc_TypeError, "halfdelay requires 1 argument");
  1613.     return NULL;
  1614.   }
  1615.  
  1616.   return PyCursesCheckERR(halfdelay(tenths), "halfdelay");
  1617. }
  1618.  
  1619. #ifndef STRICT_SYSV_CURSES
  1620.  /* No has_key! */
  1621. static PyObject * PyCurses_has_key(PyObject *self, PyObject *args)
  1622. {
  1623.   int ch;
  1624.  
  1625.   PyCursesInitialised
  1626.  
  1627.   if (!PyArg_Parse(args,"i",&ch)) return NULL;
  1628.  
  1629.   if (has_key(ch) == FALSE) {
  1630.     Py_INCREF(Py_False);
  1631.     return Py_False;
  1632.   }
  1633.   Py_INCREF(Py_True);
  1634.   return Py_True; 
  1635. }
  1636. #endif /* STRICT_SYSV_CURSES */
  1637.  
  1638. static PyObject *
  1639. PyCurses_Init_Color(PyObject *self, PyObject *args)
  1640. {
  1641.   short color, r, g, b;
  1642.  
  1643.   PyCursesInitialised
  1644.   PyCursesInitialisedColor
  1645.  
  1646.   switch(ARG_COUNT(args)) {
  1647.   case 4:
  1648.     if (!PyArg_Parse(args, "(hhhh);color,r,g,b", &color, &r, &g, &b)) return NULL;
  1649.     break;
  1650.   default:
  1651.     PyErr_SetString(PyExc_TypeError, "init_color requires 4 arguments");
  1652.     return NULL;
  1653.   }
  1654.  
  1655.   return PyCursesCheckERR(init_color(color, r, g, b), "init_color");
  1656. }
  1657.  
  1658. static PyObject *
  1659. PyCurses_Init_Pair(PyObject *self, PyObject *args)
  1660. {
  1661.   short pair, f, b;
  1662.  
  1663.   PyCursesInitialised
  1664.   PyCursesInitialisedColor
  1665.  
  1666.   if (ARG_COUNT(args) != 3) {
  1667.     PyErr_SetString(PyExc_TypeError, "init_pair requires 3 arguments");
  1668.     return NULL;
  1669.   }
  1670.  
  1671.   if (!PyArg_Parse(args, "(hhh);pair, f, b", &pair, &f, &b)) return NULL;
  1672.  
  1673.   return PyCursesCheckERR(init_pair(pair, f, b), "init_pair");
  1674. }
  1675.  
  1676. static PyObject * 
  1677. PyCurses_InitScr(PyObject *self, PyObject *args)
  1678. {
  1679.   WINDOW *win;
  1680.   PyObject *lines, *cols;
  1681.  
  1682.   if (!PyArg_NoArgs(args)) return NULL;
  1683.  
  1684.   if (initialised == TRUE) {
  1685.     wrefresh(stdscr);
  1686.     return (PyObject *)PyCursesWindow_New(stdscr);
  1687.   }
  1688.  
  1689.   win = initscr();
  1690.  
  1691.   if (win == NULL) {
  1692.     PyErr_SetString(PyCursesError, catchall_NULL);
  1693.     return NULL;
  1694.   }
  1695.  
  1696.   initialised = TRUE;
  1697.  
  1698. /* This was moved from initcurses() because it core dumped on SGI,
  1699.    where they're not defined until you've called initscr() */
  1700. #define SetDictInt(string,ch) \
  1701.     PyDict_SetItemString(ModDict,string,PyInt_FromLong((long) (ch)));
  1702.  
  1703.     /* Here are some graphic symbols you can use */
  1704.         SetDictInt("ACS_ULCORNER",      (ACS_ULCORNER));
  1705.     SetDictInt("ACS_LLCORNER",      (ACS_LLCORNER));
  1706.     SetDictInt("ACS_URCORNER",      (ACS_URCORNER));
  1707.     SetDictInt("ACS_LRCORNER",      (ACS_LRCORNER));
  1708.     SetDictInt("ACS_LTEE",          (ACS_LTEE));
  1709.     SetDictInt("ACS_RTEE",          (ACS_RTEE));
  1710.     SetDictInt("ACS_BTEE",          (ACS_BTEE));
  1711.     SetDictInt("ACS_TTEE",          (ACS_TTEE));
  1712.     SetDictInt("ACS_HLINE",         (ACS_HLINE));
  1713.     SetDictInt("ACS_VLINE",         (ACS_VLINE));
  1714.     SetDictInt("ACS_PLUS",          (ACS_PLUS));
  1715.     SetDictInt("ACS_S1",            (ACS_S1));
  1716.     SetDictInt("ACS_S9",            (ACS_S9));
  1717.     SetDictInt("ACS_DIAMOND",       (ACS_DIAMOND));
  1718.     SetDictInt("ACS_CKBOARD",       (ACS_CKBOARD));
  1719.     SetDictInt("ACS_DEGREE",        (ACS_DEGREE));
  1720.     SetDictInt("ACS_PLMINUS",       (ACS_PLMINUS));
  1721.     SetDictInt("ACS_BULLET",        (ACS_BULLET));
  1722.     SetDictInt("ACS_LARROW",        (ACS_LARROW));
  1723.     SetDictInt("ACS_RARROW",        (ACS_RARROW));
  1724.     SetDictInt("ACS_DARROW",        (ACS_DARROW));
  1725.     SetDictInt("ACS_UARROW",        (ACS_UARROW));
  1726.     SetDictInt("ACS_BOARD",         (ACS_BOARD));
  1727.     SetDictInt("ACS_LANTERN",       (ACS_LANTERN));
  1728.     SetDictInt("ACS_BLOCK",         (ACS_BLOCK));
  1729.     SetDictInt("ACS_BSSB",          (ACS_ULCORNER));
  1730.     SetDictInt("ACS_SSBB",          (ACS_LLCORNER));
  1731.     SetDictInt("ACS_BBSS",          (ACS_URCORNER));
  1732.     SetDictInt("ACS_SBBS",          (ACS_LRCORNER));
  1733.     SetDictInt("ACS_SBSS",          (ACS_RTEE));
  1734.     SetDictInt("ACS_SSSB",          (ACS_LTEE));
  1735.     SetDictInt("ACS_SSBS",          (ACS_BTEE));
  1736.     SetDictInt("ACS_BSSS",          (ACS_TTEE));
  1737.     SetDictInt("ACS_BSBS",          (ACS_HLINE));
  1738.     SetDictInt("ACS_SBSB",          (ACS_VLINE));
  1739.     SetDictInt("ACS_SSSS",          (ACS_PLUS));
  1740. #ifndef STRICT_SYSV_CURSES
  1741.   /* The following are never available with strict SYSV curses */
  1742.     SetDictInt("ACS_S3",            (ACS_S3));
  1743.     SetDictInt("ACS_LEQUAL",        (ACS_LEQUAL));
  1744.     SetDictInt("ACS_GEQUAL",        (ACS_GEQUAL));
  1745.     SetDictInt("ACS_PI",            (ACS_PI));
  1746.     SetDictInt("ACS_NEQUAL",        (ACS_NEQUAL));
  1747.     SetDictInt("ACS_STERLING",      (ACS_STERLING));
  1748. #endif
  1749.  
  1750.   lines = PyInt_FromLong((long) LINES);
  1751.   PyDict_SetItemString(ModDict, "LINES", lines);
  1752.   Py_DECREF(lines);
  1753.   cols = PyInt_FromLong((long) COLS);
  1754.   PyDict_SetItemString(ModDict, "COLS", cols);
  1755.   Py_DECREF(cols);
  1756.  
  1757.   return (PyObject *)PyCursesWindow_New(win);
  1758. }
  1759.  
  1760.  
  1761. static PyObject *
  1762. PyCurses_IntrFlush(PyObject *self, PyObject *args)
  1763. {
  1764.   int ch;
  1765.  
  1766.   PyCursesInitialised
  1767.  
  1768.   switch(ARG_COUNT(args)) {
  1769.   case 1:
  1770.     if (!PyArg_Parse(args,"i;True(1), False(0)",&ch)) return NULL;
  1771.     break;
  1772.   default:
  1773.     PyErr_SetString(PyExc_TypeError, "intrflush requires 1 argument");
  1774.     return NULL;
  1775.   }
  1776.  
  1777.   return PyCursesCheckERR(intrflush(NULL,ch), "intrflush");
  1778. }
  1779.  
  1780. static PyObject *
  1781. PyCurses_KeyName(PyObject *self, PyObject *args)
  1782. {
  1783.   const char *knp;
  1784.   int ch;
  1785.  
  1786.   PyCursesInitialised
  1787.  
  1788.   if (!PyArg_Parse(args,"i",&ch)) return NULL;
  1789.  
  1790.   knp = keyname(ch);
  1791.  
  1792.   return PyString_FromString((knp == NULL) ? "" : (char *)knp);
  1793. }
  1794.  
  1795. static PyObject *  
  1796. PyCurses_KillChar(PyObject *self, PyObject *args)  
  1797. {  
  1798.   char ch;  
  1799.  
  1800.   if (!PyArg_NoArgs(args)) return NULL;  
  1801.  
  1802.   ch = killchar();  
  1803.  
  1804.   return PyString_FromString(&ch);  
  1805. }  
  1806.  
  1807. static PyObject *
  1808. PyCurses_Meta(PyObject *self, PyObject *args)
  1809. {
  1810.   int ch;
  1811.  
  1812.   PyCursesInitialised
  1813.  
  1814.   switch(ARG_COUNT(args)) {
  1815.   case 1:
  1816.     if (!PyArg_Parse(args,"i;True(1), False(0)",&ch)) return NULL;
  1817.     break;
  1818.   default:
  1819.     PyErr_SetString(PyExc_TypeError, "meta requires 1 argument");
  1820.     return NULL;
  1821.   }
  1822.  
  1823.   return PyCursesCheckERR(meta(stdscr, ch), "meta");
  1824. }
  1825.  
  1826. #ifdef NCURSES_MOUSE_VERSION
  1827. static PyObject *
  1828. PyCurses_MouseInterval(PyObject *self, PyObject *args)
  1829. {
  1830.     int interval;
  1831.     PyCursesInitialised 
  1832.  
  1833.     if (!PyArg_Parse(args,"i;interval",&interval)) 
  1834.         return NULL;
  1835.     return PyCursesCheckERR(mouseinterval(interval), "mouseinterval");
  1836. }
  1837.  
  1838. static PyObject *
  1839. PyCurses_MouseMask(PyObject *self, PyObject *args)
  1840. {
  1841.     int newmask;
  1842.     mmask_t oldmask, availmask;
  1843.  
  1844.     PyCursesInitialised 
  1845.     if (!PyArg_Parse(args,"i;mousemask",&newmask)) 
  1846.         return NULL;
  1847.     availmask = mousemask(newmask, &oldmask);
  1848.     return Py_BuildValue("(ll)", (long)availmask, (long)oldmask);
  1849. }
  1850. #endif
  1851.  
  1852. static PyObject *
  1853. PyCurses_NewPad(PyObject *self, PyObject *args)
  1854. {
  1855.   WINDOW *win;
  1856.   int nlines, ncols;
  1857.  
  1858.   PyCursesInitialised 
  1859.  
  1860.   if (!PyArg_Parse(args,"(ii);nlines,ncols",&nlines,&ncols)) return NULL;
  1861.  
  1862.   win = newpad(nlines, ncols);
  1863.   
  1864.   if (win == NULL) {
  1865.     PyErr_SetString(PyCursesError, catchall_NULL);
  1866.     return NULL;
  1867.   }
  1868.  
  1869.   return (PyObject *)PyCursesWindow_New(win);
  1870. }
  1871.  
  1872. static PyObject *
  1873. PyCurses_NewWindow(PyObject *self, PyObject *args)
  1874. {
  1875.   WINDOW *win;
  1876.   int nlines, ncols, begin_y, begin_x;
  1877.  
  1878.   PyCursesInitialised
  1879.  
  1880.   switch (ARG_COUNT(args)) {
  1881.   case 2:
  1882.     if (!PyArg_Parse(args,"(ii);nlines,ncols",&nlines,&ncols))
  1883.       return NULL;
  1884.     win = newpad(nlines, ncols);
  1885.     break;
  1886.   case 4:
  1887.     if (!PyArg_Parse(args, "(iiii);nlines,ncols,begin_y,begin_x",
  1888.            &nlines,&ncols,&begin_y,&begin_x))
  1889.       return NULL;
  1890.     win = newwin(nlines,ncols,begin_y,begin_x);
  1891.     break;
  1892.   default:
  1893.     PyErr_SetString(PyExc_TypeError, "newwin requires 2 or 4 arguments");
  1894.     return NULL;
  1895.   }
  1896.  
  1897.   if (win == NULL) {
  1898.     PyErr_SetString(PyCursesError, catchall_NULL);
  1899.     return NULL;
  1900.   }
  1901.  
  1902.   return (PyObject *)PyCursesWindow_New(win);
  1903. }
  1904.  
  1905. static PyObject *
  1906. PyCurses_Pair_Content(PyObject *self, PyObject *args)
  1907. {
  1908.   short pair,f,b;
  1909.  
  1910.   PyCursesInitialised
  1911.   PyCursesInitialisedColor
  1912.  
  1913.   switch(ARG_COUNT(args)) {
  1914.   case 1:
  1915.     if (!PyArg_Parse(args, "h;pair", &pair)) return NULL;
  1916.     break;
  1917.   default:
  1918.     PyErr_SetString(PyExc_TypeError, "pair_content requires 1 argument");
  1919.     return NULL;
  1920.   }
  1921.  
  1922.   if (!pair_content(pair, &f, &b)) {
  1923.     PyErr_SetString(PyCursesError,
  1924.             "Argument 1 was out of range. (1..COLOR_PAIRS-1)");
  1925.     return NULL;
  1926.   }
  1927.  
  1928.   return Py_BuildValue("(ii)", f, b);
  1929. }
  1930.  
  1931. static PyObject *
  1932. PyCurses_pair_number(PyObject *self, PyObject *args)
  1933. {
  1934.   int n;
  1935.  
  1936.   PyCursesInitialised
  1937.   PyCursesInitialisedColor
  1938.  
  1939.   switch(ARG_COUNT(args)) {
  1940.   case 1:
  1941.     if (!PyArg_Parse(args, "i;pairvalue", &n)) return NULL;
  1942.     break;
  1943.   default:
  1944.     PyErr_SetString(PyExc_TypeError,
  1945.                     "pair_number requires 1 argument");
  1946.     return NULL;
  1947.   }
  1948.  
  1949.   return PyInt_FromLong((long) ((n & A_COLOR) >> 8));
  1950. }
  1951.  
  1952. static PyObject *
  1953. PyCurses_Putp(PyObject *self, PyObject *args)
  1954. {
  1955.   char *str;
  1956.  
  1957.   if (!PyArg_Parse(args,"s;str", &str)) return NULL;
  1958.   return PyCursesCheckERR(putp(str), "putp");
  1959. }
  1960.  
  1961. static PyObject *
  1962. PyCurses_QiFlush(PyObject *self, PyObject *args)
  1963. {
  1964.   int flag = 0;
  1965.  
  1966.   PyCursesInitialised
  1967.  
  1968.   switch(ARG_COUNT(args)) {
  1969.   case 0:
  1970.     qiflush();
  1971.     Py_INCREF(Py_None);
  1972.     return Py_None;
  1973.   case 1:
  1974.     if (!PyArg_Parse(args, "i;True(1) or False(0)", &flag)) return NULL;
  1975.     if (flag) qiflush();
  1976.     else noqiflush();
  1977.     Py_INCREF(Py_None);
  1978.     return Py_None;
  1979.   default:
  1980.     PyErr_SetString(PyExc_TypeError, "qiflush requires 0 or 1 arguments");
  1981.     return NULL;
  1982.   }
  1983. }
  1984.  
  1985. static PyObject *
  1986. PyCurses_setsyx(PyObject *self, PyObject *args)
  1987. {
  1988.   int y,x;
  1989.  
  1990.   PyCursesInitialised
  1991.  
  1992.   if (ARG_COUNT(args)!=2) {
  1993.     PyErr_SetString(PyExc_TypeError, "setsyx requires 2 arguments");
  1994.     return NULL;
  1995.   }
  1996.  
  1997.   if (!PyArg_Parse(args, "(ii);y, x", &y, &x)) return NULL;
  1998.  
  1999.   setsyx(y,x);
  2000.  
  2001.   Py_INCREF(Py_None);
  2002.   return Py_None;
  2003. }
  2004.  
  2005. static PyObject *
  2006. PyCurses_Start_Color(PyObject *self, PyObject *args)
  2007. {
  2008.   int code;
  2009.   PyObject *c, *cp;
  2010.  
  2011.   PyCursesInitialised
  2012.  
  2013.   if (!PyArg_NoArgs(args)) return NULL;
  2014.  
  2015.   code = start_color();
  2016.   if (code != ERR) {
  2017.     initialisedcolors = TRUE;
  2018.     c = PyInt_FromLong((long) COLORS);
  2019.     PyDict_SetItemString(ModDict, "COLORS", c);
  2020.     Py_DECREF(c);
  2021.     cp = PyInt_FromLong((long) COLOR_PAIRS);
  2022.     PyDict_SetItemString(ModDict, "COLOR_PAIRS", cp);
  2023.     Py_DECREF(cp);
  2024.     Py_INCREF(Py_None);
  2025.     return Py_None;
  2026.   } else {
  2027.     PyErr_SetString(PyCursesError, "start_color() returned ERR");
  2028.     return NULL;
  2029.   }
  2030. }
  2031.  
  2032. static PyObject *
  2033. PyCurses_tigetflag(PyObject *self, PyObject *args)
  2034. {
  2035.     char *capname;
  2036.  
  2037.     PyCursesInitialised;
  2038.         
  2039.     if (!PyArg_ParseTuple(args, "z", &capname))
  2040.         return NULL;
  2041.  
  2042.     return PyInt_FromLong( (long) tigetflag( capname ) );
  2043. }
  2044.  
  2045. static PyObject *
  2046. PyCurses_tigetnum(PyObject *self, PyObject *args)
  2047. {
  2048.     char *capname;
  2049.  
  2050.     PyCursesInitialised;
  2051.         
  2052.     if (!PyArg_ParseTuple(args, "z", &capname))
  2053.         return NULL;
  2054.  
  2055.     return PyInt_FromLong( (long) tigetnum( capname ) );
  2056. }
  2057.  
  2058. static PyObject *
  2059. PyCurses_tigetstr(PyObject *self, PyObject *args)
  2060. {
  2061.     char *capname;
  2062.  
  2063.     PyCursesInitialised;
  2064.         
  2065.     if (!PyArg_ParseTuple(args, "z", &capname))
  2066.         return NULL;
  2067.  
  2068.     capname = tigetstr( capname );
  2069.     if (capname == 0 || capname == (char*) -1) {
  2070.         Py_INCREF(Py_None);
  2071.         return Py_None;
  2072.     }
  2073.     return PyString_FromString( capname );
  2074. }
  2075.  
  2076. static PyObject *
  2077. PyCurses_TypeAhead(PyObject *self, PyObject *args)
  2078. {
  2079.   int fd;
  2080.  
  2081.   PyCursesInitialised
  2082.  
  2083.   if (!PyArg_Parse(args,"i;fd",&fd)) return NULL;
  2084.  
  2085.   PyCursesCheckERR(typeahead( fd ), "typeahead");
  2086.   Py_INCREF(Py_None);
  2087.   return Py_None;
  2088. }
  2089.  
  2090. static PyObject *
  2091. PyCurses_UnCtrl(PyObject *self, PyObject *args)
  2092. {
  2093.   PyObject *temp;
  2094.   chtype ch;
  2095.  
  2096.   PyCursesInitialised
  2097.  
  2098.   if (!PyArg_Parse(args,"O;ch or int",&temp)) return NULL;
  2099.  
  2100.   if (PyInt_Check(temp))
  2101.     ch = (chtype) PyInt_AsLong(temp);
  2102.   else if (PyString_Check(temp))
  2103.     ch = (chtype) *PyString_AsString(temp);
  2104.   else {
  2105.     PyErr_SetString(PyExc_TypeError, "argument must be a ch or an int");
  2106.     return NULL;
  2107.   }
  2108.  
  2109.   return PyString_FromString(unctrl(ch));
  2110. }
  2111.  
  2112. static PyObject *
  2113. PyCurses_UngetCh(PyObject *self, PyObject *args)
  2114. {
  2115.   PyObject *temp;
  2116.   chtype ch;
  2117.  
  2118.   PyCursesInitialised
  2119.  
  2120.   if (!PyArg_Parse(args,"O;ch or int",&temp)) return NULL;
  2121.  
  2122.   if (PyInt_Check(temp))
  2123.     ch = (chtype) PyInt_AsLong(temp);
  2124.   else if (PyString_Check(temp))
  2125.     ch = (chtype) *PyString_AsString(temp);
  2126.   else {
  2127.     PyErr_SetString(PyExc_TypeError, "argument must be a ch or an int");
  2128.     return NULL;
  2129.   }
  2130.  
  2131.   return PyCursesCheckERR(ungetch(ch), "ungetch");
  2132. }
  2133.  
  2134. static PyObject *
  2135. PyCurses_Use_Env(PyObject *self, PyObject *args)
  2136. {
  2137.   int flag;
  2138.  
  2139.   PyCursesInitialised
  2140.  
  2141.   switch(ARG_COUNT(args)) {
  2142.   case 1:
  2143.     if (!PyArg_Parse(args,"i;True(1), False(0)",&flag))
  2144.       return NULL;
  2145.     break;
  2146.   default:
  2147.     PyErr_SetString(PyExc_TypeError, "use_env requires 1 argument");
  2148.     return NULL;
  2149.   }
  2150.   use_env(flag);
  2151.   Py_INCREF(Py_None);
  2152.   return Py_None;
  2153. }
  2154.  
  2155. /* List of functions defined in the module */
  2156.  
  2157. static PyMethodDef PyCurses_methods[] = {
  2158.   {"baudrate",            (PyCFunction)PyCurses_baudrate},
  2159.   {"beep",                (PyCFunction)PyCurses_beep},
  2160.   {"can_change_color",    (PyCFunction)PyCurses_can_change_color},
  2161.   {"cbreak",              (PyCFunction)PyCurses_cbreak},
  2162.   {"color_content",       (PyCFunction)PyCurses_Color_Content},
  2163.   {"color_pair",          (PyCFunction)PyCurses_color_pair},
  2164.   {"curs_set",            (PyCFunction)PyCurses_Curs_Set},
  2165.   {"def_prog_mode",       (PyCFunction)PyCurses_def_prog_mode},
  2166.   {"def_shell_mode",      (PyCFunction)PyCurses_def_shell_mode},
  2167.   {"delay_output",        (PyCFunction)PyCurses_Delay_Output},
  2168.   {"doupdate",            (PyCFunction)PyCurses_doupdate},
  2169.   {"echo",                (PyCFunction)PyCurses_echo},
  2170.   {"endwin",              (PyCFunction)PyCurses_endwin},
  2171.   {"erasechar",           (PyCFunction)PyCurses_EraseChar},
  2172.   {"filter",              (PyCFunction)PyCurses_filter},
  2173.   {"flash",               (PyCFunction)PyCurses_flash},
  2174.   {"flushinp",            (PyCFunction)PyCurses_flushinp},
  2175. #ifdef NCURSES_MOUSE_VERSION
  2176.   {"getmouse",            (PyCFunction)PyCurses_GetMouse},
  2177.   {"ungetmouse",          (PyCFunction)PyCurses_UngetMouse, METH_VARARGS},
  2178. #endif
  2179.   {"getsyx",              (PyCFunction)PyCurses_getsyx},
  2180.   {"getwin",              (PyCFunction)PyCurses_GetWin},
  2181.   {"has_colors",          (PyCFunction)PyCurses_has_colors},
  2182.   {"has_ic",              (PyCFunction)PyCurses_has_ic},
  2183.   {"has_il",              (PyCFunction)PyCurses_has_il},
  2184. #ifndef STRICT_SYSV_CURSES
  2185.   {"has_key",             (PyCFunction)PyCurses_has_key},
  2186. #endif
  2187.   {"halfdelay",           (PyCFunction)PyCurses_HalfDelay},
  2188.   {"init_color",          (PyCFunction)PyCurses_Init_Color},
  2189.   {"init_pair",           (PyCFunction)PyCurses_Init_Pair},
  2190.   {"initscr",             (PyCFunction)PyCurses_InitScr},
  2191.   {"intrflush",           (PyCFunction)PyCurses_IntrFlush},
  2192.   {"isendwin",            (PyCFunction)PyCurses_isendwin},
  2193.   {"keyname",             (PyCFunction)PyCurses_KeyName},
  2194.   {"killchar",            (PyCFunction)PyCurses_KillChar}, 
  2195.   {"longname",            (PyCFunction)PyCurses_longname}, 
  2196.   {"meta",                (PyCFunction)PyCurses_Meta},
  2197. #ifdef NCURSES_MOUSE_VERSION
  2198.   {"mouseinterval",       (PyCFunction)PyCurses_MouseInterval},
  2199.   {"mousemask",           (PyCFunction)PyCurses_MouseMask},
  2200. #endif
  2201.   {"newpad",              (PyCFunction)PyCurses_NewPad},
  2202.   {"newwin",              (PyCFunction)PyCurses_NewWindow},
  2203.   {"nl",                  (PyCFunction)PyCurses_nl},
  2204.   {"nocbreak",            (PyCFunction)PyCurses_nocbreak},
  2205.   {"noecho",              (PyCFunction)PyCurses_noecho},
  2206.   {"nonl",                (PyCFunction)PyCurses_nonl},
  2207.   {"noqiflush",           (PyCFunction)PyCurses_noqiflush},
  2208.   {"noraw",               (PyCFunction)PyCurses_noraw},
  2209.   {"pair_content",        (PyCFunction)PyCurses_Pair_Content},
  2210.   {"pair_number",         (PyCFunction)PyCurses_pair_number},
  2211.   {"putp",                (PyCFunction)PyCurses_Putp},
  2212.   {"qiflush",             (PyCFunction)PyCurses_QiFlush},
  2213.   {"raw",                 (PyCFunction)PyCurses_raw},
  2214.   {"reset_prog_mode",     (PyCFunction)PyCurses_reset_prog_mode},
  2215.   {"reset_shell_mode",    (PyCFunction)PyCurses_reset_shell_mode},
  2216.   {"resetty",             (PyCFunction)PyCurses_resetty},
  2217.   {"savetty",             (PyCFunction)PyCurses_savetty},
  2218.   {"setsyx",              (PyCFunction)PyCurses_setsyx},
  2219.   {"start_color",         (PyCFunction)PyCurses_Start_Color},
  2220.   {"termattrs",           (PyCFunction)PyCurses_termattrs},
  2221.   {"termname",            (PyCFunction)PyCurses_termname},
  2222.   {"tigetflag",          (PyCFunction)PyCurses_tigetflag, METH_VARARGS},
  2223.   {"tigetnum",          (PyCFunction)PyCurses_tigetnum, METH_VARARGS},
  2224.   {"tigetstr",          (PyCFunction)PyCurses_tigetstr, METH_VARARGS},
  2225.   {"typeahead",           (PyCFunction)PyCurses_TypeAhead},
  2226.   {"unctrl",              (PyCFunction)PyCurses_UnCtrl},
  2227.   {"ungetch",             (PyCFunction)PyCurses_UngetCh},
  2228.   {"use_env",             (PyCFunction)PyCurses_Use_Env},
  2229.   {NULL,        NULL}        /* sentinel */
  2230. };
  2231.  
  2232. /* Initialization function for the module */
  2233.  
  2234. void
  2235. init_curses(void)
  2236. {
  2237.     PyObject *m, *d, *v;
  2238.  
  2239.     /* Create the module and add the functions */
  2240.     m = Py_InitModule("_curses", PyCurses_methods);
  2241.  
  2242.     /* Add some symbolic constants to the module */
  2243.     d = PyModule_GetDict(m);
  2244.     ModDict = d; /* For PyCurses_InitScr */
  2245.  
  2246.     /* For exception curses.error */
  2247.     PyCursesError = PyErr_NewException("_curses.error", NULL, NULL);
  2248.     PyDict_SetItemString(d, "error", PyCursesError);
  2249.  
  2250.     /* Make the version available */
  2251.     v = PyString_FromString(PyCursesVersion);
  2252.     PyDict_SetItemString(d, "version", v);
  2253.     PyDict_SetItemString(d, "__version__", v);
  2254.     Py_DECREF(v);
  2255.  
  2256.     /* Here are some attributes you can add to chars to print */
  2257.     
  2258.     SetDictInt("A_ATTRIBUTES",      A_ATTRIBUTES);
  2259.     SetDictInt("A_NORMAL",        A_NORMAL);
  2260.     SetDictInt("A_STANDOUT",    A_STANDOUT);
  2261.     SetDictInt("A_UNDERLINE",    A_UNDERLINE);
  2262.     SetDictInt("A_REVERSE",        A_REVERSE);
  2263.     SetDictInt("A_BLINK",        A_BLINK);
  2264.     SetDictInt("A_DIM",        A_DIM);
  2265.     SetDictInt("A_BOLD",        A_BOLD);
  2266.     SetDictInt("A_ALTCHARSET",    A_ALTCHARSET);
  2267.     SetDictInt("A_INVIS",           A_INVIS);
  2268.     SetDictInt("A_PROTECT",         A_PROTECT);
  2269.     SetDictInt("A_CHARTEXT",        A_CHARTEXT);
  2270.     SetDictInt("A_COLOR",           A_COLOR);
  2271. #ifndef STRICT_SYSV_CURSES
  2272.     SetDictInt("A_HORIZONTAL",      A_HORIZONTAL);
  2273.     SetDictInt("A_LEFT",            A_LEFT);
  2274.     SetDictInt("A_LOW",             A_LOW);
  2275.     SetDictInt("A_RIGHT",           A_RIGHT);
  2276.     SetDictInt("A_TOP",             A_TOP);
  2277.     SetDictInt("A_VERTICAL",        A_VERTICAL);
  2278. #endif
  2279.  
  2280.     SetDictInt("COLOR_BLACK",       COLOR_BLACK);
  2281.     SetDictInt("COLOR_RED",         COLOR_RED);
  2282.     SetDictInt("COLOR_GREEN",       COLOR_GREEN);
  2283.     SetDictInt("COLOR_YELLOW",      COLOR_YELLOW);
  2284.     SetDictInt("COLOR_BLUE",        COLOR_BLUE);
  2285.     SetDictInt("COLOR_MAGENTA",     COLOR_MAGENTA);
  2286.     SetDictInt("COLOR_CYAN",        COLOR_CYAN);
  2287.     SetDictInt("COLOR_WHITE",       COLOR_WHITE);
  2288.  
  2289. #ifdef NCURSES_MOUSE_VERSION
  2290.     /* Mouse-related constants */
  2291.     SetDictInt("BUTTON1_PRESSED",          BUTTON1_PRESSED);
  2292.     SetDictInt("BUTTON1_RELEASED",         BUTTON1_RELEASED);
  2293.     SetDictInt("BUTTON1_CLICKED",          BUTTON1_CLICKED);
  2294.     SetDictInt("BUTTON1_DOUBLE_CLICKED",   BUTTON1_DOUBLE_CLICKED);
  2295.     SetDictInt("BUTTON1_TRIPLE_CLICKED",   BUTTON1_TRIPLE_CLICKED);
  2296.  
  2297.     SetDictInt("BUTTON2_PRESSED",          BUTTON2_PRESSED);
  2298.     SetDictInt("BUTTON2_RELEASED",         BUTTON2_RELEASED);
  2299.     SetDictInt("BUTTON2_CLICKED",          BUTTON2_CLICKED);
  2300.     SetDictInt("BUTTON2_DOUBLE_CLICKED",   BUTTON2_DOUBLE_CLICKED);
  2301.     SetDictInt("BUTTON2_TRIPLE_CLICKED",   BUTTON2_TRIPLE_CLICKED);
  2302.  
  2303.     SetDictInt("BUTTON3_PRESSED",          BUTTON3_PRESSED);
  2304.     SetDictInt("BUTTON3_RELEASED",         BUTTON3_RELEASED);
  2305.     SetDictInt("BUTTON3_CLICKED",          BUTTON3_CLICKED);
  2306.     SetDictInt("BUTTON3_DOUBLE_CLICKED",   BUTTON3_DOUBLE_CLICKED);
  2307.     SetDictInt("BUTTON3_TRIPLE_CLICKED",   BUTTON3_TRIPLE_CLICKED);
  2308.  
  2309.     SetDictInt("BUTTON4_PRESSED",          BUTTON4_PRESSED);
  2310.     SetDictInt("BUTTON4_RELEASED",         BUTTON4_RELEASED);
  2311.     SetDictInt("BUTTON4_CLICKED",          BUTTON4_CLICKED);
  2312.     SetDictInt("BUTTON4_DOUBLE_CLICKED",   BUTTON4_DOUBLE_CLICKED);
  2313.     SetDictInt("BUTTON4_TRIPLE_CLICKED",   BUTTON4_TRIPLE_CLICKED);
  2314.  
  2315.     SetDictInt("BUTTON_SHIFT",             BUTTON_SHIFT);
  2316.     SetDictInt("BUTTON_CTRL",              BUTTON_CTRL);
  2317.     SetDictInt("BUTTON_ALT",               BUTTON_ALT);
  2318.  
  2319.     SetDictInt("ALL_MOUSE_EVENTS",         ALL_MOUSE_EVENTS);
  2320.     SetDictInt("REPORT_MOUSE_POSITION",    REPORT_MOUSE_POSITION);
  2321. #endif
  2322.     /* Now set everything up for KEY_ variables */
  2323.     {
  2324.       int key;
  2325.       char *key_n;
  2326.       char *key_n2;
  2327.       for (key=KEY_MIN;key < KEY_MAX; key++) {
  2328.         key_n = (char *)keyname(key);
  2329.         if (key_n == NULL || strcmp(key_n,"UNKNOWN KEY")==0)
  2330.           continue;
  2331.         if (strncmp(key_n,"KEY_F(",6)==0) {
  2332.           char *p1, *p2;
  2333.           key_n2 = malloc(strlen(key_n)+1);
  2334.           p1 = key_n;
  2335.           p2 = key_n2;
  2336.           while (*p1) {
  2337.         if (*p1 != '(' && *p1 != ')') {
  2338.           *p2 = *p1;
  2339.           p2++;
  2340.         }
  2341.         p1++;
  2342.           }
  2343.           *p2 = (char)0;
  2344.         } else
  2345.           key_n2 = key_n;
  2346.         PyDict_SetItemString(d,key_n2,PyInt_FromLong((long) key));
  2347.         if (key_n2 != key_n)
  2348.           free(key_n2);
  2349.       }
  2350.       SetDictInt("KEY_MIN", KEY_MIN);
  2351.       SetDictInt("KEY_MAX", KEY_MAX);
  2352.     }
  2353.  
  2354.     /* Check for errors */
  2355.     if (PyErr_Occurred())
  2356.         Py_FatalError("can't initialize module _curses");
  2357. }
  2358.